【问题标题】:Neither BindingResult nor plain target object for bean name 'userProfile' available as request attributeBean 名称“userProfile”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2011-11-25 04:04:22
【问题描述】:

我在尝试实现我的第一个 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)
    ...

用户控制器.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>

【问题讨论】:

    标签: java hibernate spring exception


    【解决方案1】:
    @ModelAttribute("userProfile")
    public UserProfile getProfile(){
        return new UserProfile();
    }
    
    <form:form method="post" action="add" modelAttribute="userProfile">
    

    【讨论】:

    • 如果你能在你的答案中添加几句话来解释它是如何工作的,那就太好了。
    • Spring 寻找实例(UserProfile),用于从表单推送所有数据,在这种情况下,我们创建此实例并在 'modelAttribute="userProfile"' 表单参数与我们的方法相关,并带有注释(@ModelAttribute("用户资料”))。在我的情况下它对我有用它是用户和项目)))
    【解决方案2】:

    将此添加到您的控制器应该可以解决它

    model.addAttribute(new UserProfile());
    

    【讨论】:

      【解决方案3】:

      modelAttribute="userProfile" 添加到&lt;form:form&gt; 标记中。

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

      【讨论】:

        【解决方案4】:

        我没有注意到我必须实现表单创建方法,该方法将提供 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";
        }
        

        【讨论】:

          【解决方案5】:

          尝试在@ModelAttribute("userProfile") UserProfile userProfile旁边添加BindingResult作为方法参数

          Spring 在每个 @ModelAttribute 之后查找 BindingResult 参数

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-23
            • 2015-08-31
            • 2013-04-04
            • 2019-10-29
            • 2013-08-15
            • 1970-01-01
            相关资源
            最近更新 更多