1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Spring MVC @ModelAttribute 数据绑定

Spring MVC @ModelAttribute 数据绑定

时间:2019-08-16 12:12:14

相关推荐

Spring MVC @ModelAttribute 数据绑定

前言

示例未经验证

Spring MVC @ModelAttribute 数据绑定

通过WebDataBinder@RequestMapping方法的参数request进行数据绑定WebDataBinderrequest 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。