java bean 名称“userProfile”的 BindingResult 和普通目标对象都不能用作请求属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7554795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Neither BindingResult nor plain target object for bean name 'userProfile' available as request attribute
提问by Fedor Skrynnikov
I've encountered the folowing exception while I was trying to implement my first spring+hibernate web app:
我在尝试实现我的第一个 spring+hibernate web 应用程序时遇到了以下异常:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userProfile' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
...
UserController.java:
用户控制器.java:
@Controller
public class UserController {
@Autowired
private UserProfileService userProfileService;
public UserController(){
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public String registerUser(@ModelAttribute("userProfile") UserProfile userProfile, BindingResult result, Map model){
userProfileService.addUserProfile(userProfile);
return "redirect:/login";
}
...
}
UserProfile.java
用户配置文件
@Entity
@Table(name="USER_PROFILE")
public class UserProfile {
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "USERNAME")
private String userName;
@Column(name = "PASSWORD")
private String password;
//sets and gets
}
index.jsp
索引.jsp
<form:form method="post" action="add" commandName="userProfile">
<table>
<tr>
<td><form:label path="userName"><spring:message code="label.username" /></form:label></td>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td><form:label path="password"><spring:message code="label.password" /></form:label></td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="<spring:message code="label.adduser" />"></td>
</tr>
</table>
</form:form>
回答by Fedor Skrynnikov
I wasn't noticed that I have to implement method for form creation which will provide instance of UserProfile. I've added 2 methods and now everything works fine.
我没有注意到我必须实现表单创建方法,该方法将提供 UserProfile 的实例。我添加了 2 种方法,现在一切正常。
@RequestMapping("/")
public String home() {
return "redirect:/index";
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String createRegisterForm(Map<String, Object> model){
model.put("userprofile", new UserProfile());
return "index";
}
回答by parimala
Add modelAttribute="userProfile"
to the <form:form>
tag.
添加modelAttribute="userProfile"
到<form:form>
标签。
<form:form method="post" action="add" commandName="userProfile" modelAttribute="userProfile">
回答by user1532775
Adding this to your controller should fix it
将此添加到您的控制器应该修复它
model.addAttribute(new UserProfile());
回答by MolodecSerg
@ModelAttribute("userProfile")
public UserProfile getProfile(){
return new UserProfile();
}
<form:form method="post" action="add" modelAttribute="userProfile">
回答by Mahmoud Saleh
Try adding BindingResult as a method parameter next to @ModelAttribute("userProfile") UserProfile userProfile
尝试添加 BindingResult 作为旁边的方法参数 @ModelAttribute("userProfile") UserProfile userProfile
Spring looks for BindingResult parameter after each @ModelAttribute
Spring 在每个 @ModelAttribute 之后寻找 BindingResult 参数