Bean名称'userProfile'的BindingResult和普通目标对象都不可用作请求属性 [英] Neither BindingResult nor plain target object for bean name 'userProfile' available as request attribute

查看:2387
本文介绍了Bean名称'userProfile'的BindingResult和普通目标对象都不可用作请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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>


推荐答案

我没有注意到我必须实现方法表单创建将提供UserProfile的实例。

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

这篇关于Bean名称'userProfile'的BindingResult和普通目标对象都不可用作请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆