基本Spring MVC数据绑定 [英] Basic Spring MVC Data Binding

查看:109
本文介绍了基本Spring MVC数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring MVC,我看到无处不在,只是一个基本的控制器来查看数据绑定,但没有我曾经尝试过的工作。我可以将视图发布回控制器,我可以看到具有属性的pojo,但是无论何时我试图将该对象添加到模型中,我什么都没有。这是我到目前为止:



控制器

  @Controller 
public class HomeController {

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

model.addAttribute(new Person());
返回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;
私人日期_Birthday;

//设置
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-Typecontent =text / html; charset = ISO-8859- 1/> 
< html>
< / head>
< body>
FirstName:$ {model.person.getFirstName}
LastName:$ {model.person.getLastName}
< / body>
< / html>

我可以或需要这样做才能绑定?

解决方案

模型属性是你在这里缺少的东西。

  @Controller 
public class HomeController {

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

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

@RequestMapping(value =/ about,method = RequestMethod.POST)
public void about(@ModelAttribute(person)Person person,BindingResult result,模型模型)
{
if(!result.hasErrors()){
//注意我还没有编译这个代码:)
}
}
}

这个想法是在GET和POST两端将调用@ModelAttribute方法在GET请求中,它将被暴露给视图,在POST中它将用于绑定请求参数。



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


I'm learning Spring MVC and I've looked everywhere to do just a basic controller to view data binding but nothing I've tried as work. I can bind view posting back to controller and I can see the pojo with properties there, however whenever I tried to add that object to the model I get nothing. Here is what I have so far:

Controller

@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);
    }   
 }

Class I want to bind

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

View - Controller-to-Form ! Working

<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>

What can I or need this to do get it to bind?

解决方案

The model attribute is the thing you are missing here.

@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 :)
        } 
    }   
 }

The idea is that the @ModelAttribute method will be invoked on both the GET and the POST, on the GET request it will just be exposed to the view where as on the POST it will be used to bind the request parameters.

Note that the BindingResult is passed to the POST method, so that you can do something with the command.

这篇关于基本Spring MVC数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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