2012-04-17 71 views
7

我正在学习Spring MVC,并且我到处都只是做一个基本的控制器来查看数据绑定,但没有任何我已经尝试过的工作。我可以将视图张贴回控制器,我可以在那里看到带有属性的pojo,但是无论何时我试图将该对象添加到模型中,我什么也得不到。以下是我迄今为止:基本的Spring MVC数据绑定

控制器

@Controller 
public class HomeController { 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Model model) { 

     model.addAttribute(new Person()); 
     return "home"; 
    } 

    @RequestMapping(value="/about", method=RequestMethod.POST) 
    public void about(Person person, Model model) 
    { 
     model.addAttribute("person", person); 
    } 
} 

类我要绑定

public class Person { 
private String _firstName; 
private String _lastName; 
private Date _Birthday; 

//Set 
public void setFirstName(String FirstName){this._firstName = FirstName; } 
public void setLastName(String LastName){this._lastName= LastName; } 
public void setBirthDate(Date BirthDate){ this._Birthday = BirthDate;} 

//get 
public String getFirstName(){return _firstName;} 
public String getLastName(){return _lastName;} 
public Date getBirthDate(){return _Birthday;} 
} 

视图 - 控制器对表!工作

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<html> 
</head> 
    <body> 
     FirstName: ${model.person.getFirstName} 
     LastName: ${model.person.getLastName} 
    </body> 
</html> 

我可以或需要这个做什么让它绑定?

回答

7

模型属性是你在这里想念的东西。

@Controller 
public class HomeController { 

    @ModelAttribute("person") 
    public Person getPerson(){ 
     return new Person();   
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home() { 
     return "home"; 
    } 

    @RequestMapping(value="/about", method=RequestMethod.POST) 
    public void about(@ModelAttribute("person") Person person, BindingResult result, Model model) 
    { 
     if(! result.hasErrors()){ 
      // note I haven't compiled this code :) 
     } 
    } 
} 

的想法是,该方法@ModelAttribute将在GET和POST的两个被调用,在GET请求它只会被暴露于其中作为对POST它将被用于结合的视图请求参数。

请注意,BindingResult传递给POST方法,以便您可以使用该命令执行某些操作。

1

1)modelMap的内容隐含在JSP中。访问它们时不需要指定“模型”。

2)JSP-EL通过bean属性访问器访问字段,而不是调用方法。你不指定'get'来调用一个实际的方法。您使用bean属性名称。例如$ {person.firstName}来获得person.getFirstName()的结果;

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String home(Model model) { 
    Person person = new Person(); 
    person.setFirstName("Kai"); 
    person.setLastName("Cooper"); 
    model.addAttribute("person", person); 
    return "home"; 
} 

`

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<html> 
</head> 
    <body> 
     FirstName: ${person.firstName} 
     LastName: ${person.lastName} 
    </body> 
</html> 
+0

'<形式:表格动作= “about.html” 命令名= “人”> <形式:输入路径= “姓”/> <形式:输入路径= “名字”/> < input type =“submit”value =“Submit”/> ' Home.jsp,我可以从视图绑定到控制器。为完整的图片。谢谢 – 2012-04-17 22:02:09

1

您需要更改Person对象为好,属性不应该是 “_” 之前有。将“_firsName”更改为“firstName”。吸气剂,二传手很好。 Spring绑定通过属性名称到其对应的getter和setter的。

也改变你在视图中访问的方式。使用像$ {person.firstName}。你不需要“person.firstName”之前的“模型”,也不需要提及getFirstName,spring会自动为你做。

1

请检查您的Person类变量,getter和setter方法的命名。 Getter方法应该以'get'开头,第一个大写字母的变量名与其他字母相同,例如:getFirstName()和setter方法也应该像getter方法一样。如果您的getter和setter方法的命名约定不同,则绑定无法正常工作。这是您的Person类的更新版本。

public class Person { 
    private String firstName; 
    private String lastName; 
    private Date Birthday; 

    //Set 
    public void setFirstName(String FirstName){this.firstName = FirstName; } 
    public void setLastName(String LastName){this.lastName= LastName; } 
    public void setBirthDate(Date BirthDate){ this.Birthday = BirthDate;} 

    //get 
    public String getFirstName(){return firstName;} 
    public String getLastName(){return lastName;} 
    public Date getBirthDate(){return Birthday;} 
}