2011-09-26 50 views
2

我遇到如下因素除外,而我是想實現我的第一個Spring + Hibernate的Web應用程序平原目標對象:無論BindingResult也不是爲bean名稱「USERPROFILE」可以作爲請求屬性

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> 

回答

2

我沒有注意到,我不得不實施形式創作這將法提供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"; 
} 
0

嘗試增加BindingResult的旁邊@ModelAttribute("userProfile") UserProfile userProfile

春天有個方法參數查找BindingResult參數之後的每個@ModelAttribute

+0

這樣做。沒有什麼改變 –

1

modelAttribute="userProfile"添加到<form:form>標記。

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

添加此到控制器應該修復它

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

如果你可以在你的答案中加幾句話來解釋它的工作原理,那將會很棒。 – Flexo

+0

用於從表單中提取所有數據的Spring實例(UserProfile),在這種情況下,我們創建此實例,並在'modelAttribute ='userProfile''表單參數與我們帶註釋的方法(@ModelAttribute(「userProfile」))相關。它適用於我的情況它是用戶和項目))) – MolodecSerg

相關問題