Java bean name';的BindingResult或普通目标对象;用户配置文件';可用作请求属性

Java bean name';的BindingResult或普通目标对象;用户配置文件';可用作请求属性,java,hibernate,spring,exception,Java,Hibernate,Spring,Exception,我在尝试实现我的第一个spring+hibernate web应用程序时遇到以下异常: java.lang.IllegalStateException:bean名称“userProfile”的BindingResult和普通目标对象都不能作为请求属性使用 位于org.springframework.web.servlet.support.BindStatus。(BindStatus.java:141) 位于org.springframework.web.servlet.tags.form.Abs

我在尝试实现我的第一个spring+hibernate web应用程序时遇到以下异常:

java.lang.IllegalStateException:bean名称“userProfile”的BindingResult和普通目标对象都不能作为请求属性使用
位于org.springframework.web.servlet.support.BindStatus。(BindStatus.java:141)
位于org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
位于org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
位于org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
...
UserController.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

<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>

尝试将BindingResult作为方法参数添加到
@ModelAttribute(“userProfile”)userProfile userProfile


Spring在每个@ModelAttribute之后查找BindingResult参数。我没有注意到我必须实现表单创建方法,该方法将提供UserProfile实例。我添加了两种方法,现在一切都很好

@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";
}
@RequestMapping(“/”)
公共字符串home(){
返回“重定向:/index”;
}
@RequestMapping(value=“/index”,method=RequestMethod.GET)
公共字符串CreateRegisterPerform(映射模型){
put(“userprofile”,newuserprofile());
返回“索引”;
}
modeldattribute=“userProfile”
添加到
标记中

<form:form method="post" action="add" commandName="userProfile" modelAttribute="userProfile">

将其添加到控制器中应该可以修复它

model.addAttribute(new UserProfile());

如果您能在回答中添加几句话来解释它是如何工作的,那就太好了。Spring外观的实例(UserProfile)用于从表单中推送所有数据,在本例中,我们创建了这个实例,并在“modelAttribute=“UserProfile”表单参数中使用注释(@modelAttribute(“UserProfile”))与我们的方法相关。在我的情况下,它对我有用(用户和项目)
@ModelAttribute("userProfile")
public UserProfile getProfile(){
    return new UserProfile();
}
<form:form method="post" action="add" modelAttribute="userProfile">