Map , Model , ModelMap:
暴露渲染视图需要的模型数据;
@RequestMapping("/m2")
public String method3(Map map, Model model, ModelMap modelMap,@RequestParam String name){
map.put("map",name);
model.addAttribute("model",name);
modelMap.addAttribute("modelMap",name);
return "a2";
}
<body>
<h1>${map}</h1>
<h2>${model}</h2>
<h3>${modelMap}</h3>
</body>
@RequestParam:
将请求参数数据映射到处理方法的参数上;
其拥有的主要参数:
value:参数名字(name)
required:默认为true,表示请求中一定要有参数,否则404;
defaultValue:没有参数时的默认值;
@RequestMapping("/m0")
public String method1(Map map,@RequestParam String name,@RequestParam String age){
map.put("message",name+":"+age);
return "a1";
}
请求:
<a href="/m0?name=answer&age=18">P1</a>
响应:
${message}
@PathVariable:
用于将请求URL中的模板变量映射到功能处理方法参数上;
<a href="/m1/answer/19">P2</a>
@RequestMapping("/m1/{name}/{age}")
public String method2(Map map, @PathVariable String name,@PathVariable String age){
map.put("message",name+":"+age);
return "a1";
}
@CookieValue:
将请求的Cookie映射到处理方法的参数上;
@RequestMapping("/m3")
public String method4(@CookieValue(defaultValue = "0",value = "hitcounter")int hitcounter1, HttpServletResponse response){
++hitcounter1;
Cookie cookie=new Cookie("hitcounter",hitcounter1+"");
response.addCookie(cookie);
return "cookie";
}
${cookie.hitcounter.value}
@ModelAttribute:
1.绑定请求参数到命令对象:
放在功能处理方法的入参上,可将多个请求参数绑定到一个命令对象(<a href="/m5?name=q1&password=w2">P6</a>或者是表单提交),自动暴露数据模型用于视图页面展示;
<form action="/m5">
<input type="text" name="name">
<input type="password" name="password">
<input type="submit" value="提交">
</form>
<br/>
<a href="/m5?name=q1&password=w2">P6</a>
//参数
@RequestMapping("/m5")
public String method6(@ModelAttribute("user")User user){
user.setName("name1:"+user.getName());
user.setPassword("pwd2:"+user.getPassword());
return "a3";
}
2.暴露表单引用对象为模型数据
放在处理器的一般方法上,为表单准备要展示的表单引用对象,如注册时要选择所在城市,并且在执行功能处理方法之前自动添加到模型对象中;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script src="js/jquery-1.11.0.js"></script>
<script type="text/javascript">
$(function () {
$("#modelTest").on("click",function () {
window.location.href="/modeltest";
})
})
</script>
<body>
<input type="button" id="modelTest" value="ok">
<input type="text" value="${user.name}">
<input type="text" value="${user.password}">
</body>
</html>
@ModelAttribute
public void init(Model model){
User user=new User();
user.setName("C1");
user.setPassword("D2");
model.addAttribute("user",user);
}
@RequestMapping("/modeltest")
public String method(){
return "model1";
}
<body>
<input type="text" value="${user.name}">
<input type="text" value="${user.password}">
</body>
3.暴露@RequestMapping方法返回值为模型数据:
放在功能处理方法的返回值上并暴露其为模型数据;
<form action="/m4">
<input type="text" name="name">
<input type="password" name="password">
<input type="submit" value="提交">
</form>
//返回值
@RequestMapping("/m4")
public @ModelAttribute("ansuser")User method5(User user){
user.setName("name:"+user.getName());
user.setPassword("pwd:"+user.getPassword());
return user;
}
<body>
${ansuser.name}
${ansuser.password}
</body>
对于集合类型生成的模型对象属性名为“简单类名(首字母小写)”:
List<String>:
<a href="/m7">P7</a>
@RequestMapping("/m7")
public List<String> method7(){
return Arrays.asList("A","B","C");
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${stringList}" var="ans">
${ans}
</c:forEach>
</body>
</html>
List<User>:
<a href="/m8">P8</a>
@RequestMapping("/m8")
public List<User> method8(){
return Arrays.asList(new User("n1","n2")
,new User("q1","q2"));
}
<body>
<c:forEach items="${userList}" var="user">
${user.name}
${user.password}
</c:forEach>
</body>
Map<String,User>:
<a href="/m9">P9</a>
@RequestMapping("/m9")
public @ModelAttribute Map<String,User> method9(){
Map<String,User> map=new HashMap<>();
map.put("1",new User("w1","w2"));
map.put("2",new User("e1","e2"));
return map;
}
<body>
<c:forEach items="${map}" var="ans">
${ans.key}:${ans.value.name}:${ans.value.password}
</c:forEach>
</body>