2016-04-22 45 views
0

我有一個表格:不能結合的形式變量的ModelAttribute批註的變量在Spring MVC

<form action="modelattributebinding" method="post"> 
    <input name = "student1.firstName" value = "Michael"> 
    <input name = "student1.lastName" value="Jackson" > 
    <input name="student1.age" value="34"> 
    <input name="student1.hobby" value="music"> 
    <input type="submit"> 
</form> 

並在控制器的方法,其形式員額

@RequestMapping(value = "/modelattributebinding" , 
     method = RequestMethod.POST) 
public ModelAndView binding(@ModelAttribute("student1")Student1 student1){  
    println student1.getFirstName() 
    null   
} 

我也有一個類Student1這樣

public class Student1{ 
    String firstName 
    String lastName 
    int age 
    String hobby 

    @Override 
    public String toString() { 
     "firstName $firstName , lastName $lastName" 
    } 

代碼編寫在Groovy

我的理解是,當表單提交彈簧MVC將創建基於形式的值的Student1對象,然後從表格值綁定到所述Student1對象

的例子確實的屬性不行。我的理解有什麼不正確?或者我在上面的例子中犯了什麼錯誤?

回答

0

錯誤使用替代的firstName

如果我改變我的形式看起來下面則標註的ModelAttribute不正確的結合

<form action="modelattributebinding" method="post"> 
    <input name = "firstName" value = "Michael"> 
    <input name = "lastName" value="Jackson" > 
    <input name="age" value="34"> 
    <input name="hobby" value="music"> 
    <input type="submit"> 
</form> 
student1.firstName