我在尝试配置弹簧控制器时遇到此错误。我确信我的名字正确匹配,它仍然给我这个错误。这是我的控制器类。输入和输出页面也是相同的。请帮帮我
“BindingResult和bean名称'userForm'的普通目标对象都不可用作请求属性”
@Controller
@SessionAttributes
public class UserController {
protected Log logger = LogFactory.getLog(getClass());
@Autowired
UserService userService;
@RequestMapping(value="/userList")
public ModelAndView getUserList(){
List<User> userList = userService.getUserList();
return new ModelAndView("userList", "userList", userList);
}
@RequestMapping(value="/findUser")
public ModelAndView findUser(@ModelAttribute("user") User user,
BindingResult result){
List<User> userResultsList = null;
//model.addAttribute("user", new User());
userResultsList = userService.findUser(user.getUsername(), user.getFirstname(),
user.getLastname());
return new ModelAndView("userList", "user", userResultsList);
}
}
我的JSP在这里
<body>
<form:form modelAttribute="user" method="post" action="/findUser.html">
<table>
<tr>
<td><form:label path="username">User ID:</form:label></td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name:</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="findUser" /></td>
</tr>
</table>
</form:form>
<table>
<tr>
<td>User Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>Email Address</td>
<td>Last Update Date</td>
<td>Last Update By</td>
</tr>
<c:forEach var="list" items="${userList}">
<tr>
<td>${list.userId}</td>
<td>${list.username}</td>
<td>${list.firstname}</td>
<td>${list.lastname}</td>
<td>${list.email}</td>
<td>${list.lastUpdatedBy}</td>
<td>${list.lastUpdatedDate}</td>
<td><a href="listVendors.html?userId=${list.userId}">assignVendor</a></td>
</tr>
</c:forEach>
</table>
答案 0 :(得分:0)
尝试将以下行添加到您的公共ModelAndView getUserList()方法:
modelAndView.addObject("user", new User());
答案 1 :(得分:0)
在JSP中,您使用两个名为“user”和“userList”的模型属性,它们基本上是MODEL对象的属性。
当您第一次来到页面时,Spring将尝试从Model对象(MVC中的M)中找到名称为“user”和“userList”的属性值。因此,您需要在向用户显示页面之前设置“user”和“userList”的属性值(通常从将用户带到JSP页面的方法中),在您的情况下,您可以通过更改“getUSerList”方法如下:
@RequestMapping(value="/userList")
public ModelAndView getUserList(){
ModelAndView model = new ModelAndView("userList");
User user=new User();
List<User> userList = userService.getUserList();
model.addObject("user",user);
model.addObject("userList",userList);
return model;
}
现在,此方法将带您进入“userList”JSP,其中您的MODEL填充了“user”和“userList”属性值。