前言
示例未经验证Spring MVC @ModelAttribute 数据绑定
通过WebDataBinder
将@RequestMapping
方法的参数
与request
进行数据绑定WebDataBinder
将request parameter names (query parameters and form fields)
与field names on the target Object
进行匹配数据绑定时,会使用Spring Expression Language (SpEL)
绑定属性值示例
Student 定义
class Student {private String name;private int age;private Map<String, String> param;}
Teacher 定义
class Teacher {private String name;private List<String> tag;private List<Map<String, String>> group;}
一般情况
@RequestMapping
方法声明
@PostMapping("save")public String save(Student student){}
form表单
<input type="text" name="name" /><input type="text" name="age" />
带前缀情况
@RequestMapping
方法声明
@PostMapping("save")public String save(@ModelAttribute("s")Student student, @ModelAttribute("t")Teacher teacher){}
form表单
<input type="text" name="s.name" /><input type="text" name="t.name" />
Map 作为对象属性
@RequestMapping
方法声明
@PostMapping("save")public String save(@ModelAttribute("s")Student student){}
form表单
<input type="text" name="s.param['beginDate']" /><input type="text" name="s.param['endDate']" />
List 作为对象属性
@RequestMapping
方法声明
@PostMapping("save")public String save(@ModelAttribute("t")Teacher teacher){}
form表单
<input type="text" name="t.tag[0]" /><input type="text" name="t.tag[1]" />
List<Map> 作为对象属性
@RequestMapping
方法声明
@PostMapping("save")public String save(@ModelAttribute("t")Teacher teacher){}
form表单
<input type="text" name="t.group[0]['name']" /><input type="text" name="t.group[0]['remark']" /><input type="text" name="t.group[1]['name']" /><input type="text" name="t.group[2]['name']" /><input type="text" name="t.group[2]['remark']" />
参考
/qwe6112071/article/details/51062317
/question/2539126_2160750