Neither BindingResult nor plain target object for bean name 'userProfile' available as request attribute

你离开我真会死。 提交于 2019-12-24 03:17:04

问题


I've encountered the folowing exception while I was trying to implement my first spring+hibernate web app:

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:

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

回答1:


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.

@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";
}



回答2:


Add modelAttribute="userProfile" to the <form:form> tag.

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



回答3:


Try adding BindingResult as a method parameter next to @ModelAttribute("userProfile") UserProfile userProfile

Spring looks for BindingResult parameter after each @ModelAttribute




回答4:


Adding this to your controller should fix it

model.addAttribute(new UserProfile());



回答5:


@ModelAttribute("userProfile")
public UserProfile getProfile(){
    return new UserProfile();
}
<form:form method="post" action="add" modelAttribute="userProfile">


来源:https://stackoverflow.com/questions/7554795/neither-bindingresult-nor-plain-target-object-for-bean-name-userprofile-availa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!