SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute

Spring MVC提供了以下几种途径输出模型数据:
1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据;
2)Map及Model:处理方法入参为org.springframework.ui.Model、org.springframework.ui.ModelMap或java.util.Map时,处理方法返回时,Map中的数据会自动被添加到模型中;
3)@SessionAttributes:将模型中的某个属性暂存到HttpSeession中,以便多个请求之间可以共享这个属性;
4)@ModelAttribute:方法入参标注该注解后,入参的对象就会放到数据模型中。

在SpringMVC的hanlder类中用@ModelAttribute标注的方法,会在该handler内所有目标(action)方法执行之前被SpringMVC调用。

用法示例:

Account.java

 package com.dx.springlearn.entities;

 public class Account {
public Integer id;
private String username;
private String password;
private String registerDate;
private String registerIP; public Account() {
} public Account(Integer id, String username, String password, String registerDate, String registerIP) {
super();
this.id = id;
this.username = username;
this.password = password;
this.registerDate = registerDate;
this.registerIP = registerIP;
} public Account(String username, String password, String registerDate, String registerIP) {
super();
this.username = username;
this.password = password;
this.registerDate = registerDate;
this.registerIP = registerIP;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getRegisterDate() {
return registerDate;
} public void setRegisterDate(String registerDate) {
this.registerDate = registerDate;
} public String getRegisterIP() {
return registerIP;
} public void setRegisterIP(String registerIP) {
this.registerIP = registerIP;
} @Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", password=" + password + ", registerDate="
+ registerDate + ", registerIP=" + registerIP + "]";
} }

Handler类TestModelData.java

 package com.dx.springlearn.hanlders;

 import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.dx.springlearn.entities.Account; @Controller
public class TestModelData {
private final String SUCCESS = "success"; @ModelAttribute
public void getAccount(@RequestParam(name = "id", required = false) Integer id, Map<String, Object> map) {
if (id != null) {
System.out.println("read account(id=" + id + ") from db");
Account account = new Account(1, "tommy", "123456", "2018-01-20 21:56:09", "127.0.0.1");
map.put("account", account);
} else {
System.out.println("the acount id is null");
}
} @RequestMapping("/testModelAttribute")
public ModelAndView testModelAttribute(Account account) {
System.out.println("accept account:" + account);
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
modelAndView.addObject("currentTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); return modelAndView;
}
}

测试表单页面:

    <form id="form_testModelAttribute" action="testModelAttribute"
method="POST">
<input name="id" type="hidden" value="1"/>
username:<input name="username" type="text" value="tommy" /><br>
<!-- password:<input name="password" type="password" /><br> -->
register date:<input name="registerDate" type="text" value="2018-01-20 21:56:09" /><br>
register ip:<input name="registerIP" type="text" value="127.0.0.1"/><br>
<input type="submit" value="Submit"/>
</form>

除了handler目标方法参数第一个参数名称与存放到map中的对象名称一致外,也可以使用@ModelAttribute标注handler目标方法参数:

package com.dx.springlearn.hanlders;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.dx.springlearn.entities.Account; @Controller
public class TestModelData {
private final String SUCCESS = "success"; @ModelAttribute
public void getAccount(@RequestParam(name = "id", required = false) Integer id, Map<String, Object> map) {
if (id != null) {
System.out.println("read account(id=" + id + ") from db");
Account account = new Account(1, "tommy", "123456", "2018-01-20 21:56:09", "127.0.0.1");
map.put("testabc", account);
} else {
System.out.println("the acount id is null");
}
} @RequestMapping("/testModelAttribute")
public ModelAndView testModelAttribute(@ModelAttribute("testabc") Account account) {
System.out.println("accept account:" + account);
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
modelAndView.addObject("currentTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); return modelAndView;
}
}

运行流程:

1)运行有@ModelAttribute注解标注的方法:从数据库中取出对象,把对象放入到Map中,键值为:user;

2)SpringMVC从Map中取出User对象,并把表单的请求参数赋值给该User对象的对应属性;

3)SpringMVC把上述对象传入目标方法的参数(参数名称必须与Map中的键值一致)。

注意:在@ModelAttribute修饰的方法中,放入Map是的键类型要与目标方法入参类型一直,而且Map是的键名称要与目标方法入参的参数名一致。

上一篇:能源项目xml文件 -- app-init.xml


下一篇:setter方法的内存错误