SpringMVC学习(四)-处理模型数据
2017-08-26 00:24
337 查看
处理模型数据的方式:
ModelAndView
Map
SessionAttributes注解
1.SpringMVCTest.java测试类
2.index.jsp
ModelAndView
Map
SessionAttributes注解
1.SpringMVCTest.java测试类
package com.springMVC.Helloworld; import java.util.Arrays; import java.util.Date; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; @SessionAttributes(value= {"user"},types= {String.class}) @RequestMapping("/springmvc") @Controller public class SpringMVCTest { /* * @SessionAttributes除了可以通过属性名指定主要放入会话中的属性 * 还可以通过模型的对象类型指定那些模型 需要放到会话中 */ @RequestMapping("/testSessionAttributes") public String testSessionAttributes(Map<String,Object> map) { User user=new User("Tom", "123", "123@qq.com", 12); map.put("user", user); map.put("school", "XUPT"); return "success"; } /* * 目标方法可以添加Map类型 */ @RequestMapping("/testMap") public String testMap(Map<String,Object> map) { map.put("names", Arrays.asList("Tom","Jerry","Mike")); return "success"; } /* * 目标方法返回值可以是ModelAndView类型 * 可以包含视图与模型信息 * SpringMVC会把ModelAndView的model中数据放入到request域对象中 */ @RequestMapping("/testModelAndView") public ModelAndView testModelAndView() { String viewName="success"; ModelAndView modelAndView=new ModelAndView(viewName); //添加模型数据到ModelAndView中 modelAndView.addObject("time", new Date()); return modelAndView; } @RequestMapping("/testPojo") public String testPojo(User user) { System.out.println("testPojo :"+user); return "success"; } /* * @CookieValue映射一个Cook值 */ @RequestMapping("testCookieValue") public String testCookieValue(@CookieValue("JSESSIONID") String sessionId) { System.out.println("testCookieValue:sessionId "+sessionId); return "success"; } }
2.index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <a href="springmvc/testSessionAttributes">testSessionAttributes</a> <br><br> <a href="springmvc/testMap">testMap</a> <br><br> <a href="springmvc/testModelAndView">testModelAndView</a> <br><br> <form action="springmvc/testPojo"> username:<input type="text" name="username"/> <br> password:<input type="password" name="password"/> <br> email<input type="text" name="email"/> <br> age:<input type="text" name="age"/> <br> city:<input type="text" name="address.city"> <br> province:<input type="text" name="address.province"> <br> <input type="submit" value="submit"> </form> <a href="springmvc/testCookieValue">testCookieValue</a> <br><br> </body> </html>
相关文章推荐
- 学习SpringMVC(七)之处理模型数据
- SpringMVC:学习笔记(4)——处理模型数据
- SpringMVC 学习笔记(四) 处理模型数据
- SpringMVC学习笔记(7):处理模型数据(2)
- SpringMVC学习(7):处理模型数据
- SpringMVC学习(六)之处理数据模型
- SpringMVC 学习笔记(四) 处理模型数据
- SpringMVC学习笔记(6):处理模型数据(1)
- SpringMVC 学习笔记(四) 处理模型数据
- SpringMVC处理模型数据(1)
- SpringMVC处理模型数据
- SpringMVC之处理数据模型
- SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute
- 16. SpringMVC_处理模型数据之 SessionAttributes 注解
- 10009---SpringMVC 处理模型数据
- SpringMVC 处理数据模型
- SpringMVC处理模型数据
- SpringMVC处理模型数据及SpringMVC 确定目标方法 POJO 类型入参的过程
- SpringMVC系列(六)处理模型数据
- SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes