1 //@SessionAttributes(value={"user"},types={String.class}) 2 @Controller 3 public class SpringMVC { 4 public static String SUCCESS = "success"; 5 /** 6 * 使用@RequestMapping注解来映射请求的URL 7 * @return 8 */ 9 @RequestMapping("/helloword") 10 public String testhello() { 11 System.out.println("helloword"); 12 return "success"; 13 } 14 /** 15 * 支持提交pojo 会根据name与pojo属性进行匹配 支持级联属性 16 * @param user 17 * @return 18 */ 19 @RequestMapping("/testUser") 20 public String testUser(User user) { 21 System.out.println("User:"+user.toString()); 22 return "success"; 23 } 24 /** 25 * 支持使用servlet原生api 26 * HttpServletReqyest 27 * HttpServletResponse 28 * HttpSession 29 * java.security.Prinipal 30 * Locale 31 * InputStream 32 * OutputStream 33 * Reader 34 * Writer 35 * @throws IOException 36 */ 37 @RequestMapping("testServletAPI") 38 public String testServletAPI(HttpServletRequest request, 39 HttpServletResponse response, 40 Writer out) throws IOException { 41 42 System.out.println("testServletAPI:"+request+" "+response+" "+out); 43 out.write("hello writer"); 44 out.write(1111); 45 return "success"; 46 } 47 /** 48 * ModelAndView 49 * Map及Model 50 * @sessionAttributes 51 * @ModelAttribute 52 * 目标方法的返回值可以是ModelAndView类型其中包含视图和模型信息 53 */ 54 @RequestMapping("testModelAndView") 55 public ModelAndView testModelAndView() { 56 String viewName="success"; 57 ModelAndView modelAndView = new ModelAndView(viewName); 58 modelAndView.addObject("time",new Date()); 59 return modelAndView; 60 } 61 /** 62 * 目标方法可以添加map(也可以是Model类型或ModelMap类型)类型参数 63 * @param map 64 * @return 65 */ 66 @RequestMapping("testMap") 67 public String testMap(Map<String,Object> map) { 68 map.put("names",Arrays.asList("tom","jerry","mike")); 69 return "success"; 70 } 71 /** 72 * map 默认在request域里不会装进Session域 73 * 用sessionAttributes在controller类上做注解使属性值进入session域 74 * 其括号内可放键、值 如上设置 75 * @SessionAttributes(value={"user"},types={String.class}) 76 * 表示将 键 为"user" 或 值为String类型的 键值对放入session域 77 * 注意 :该注解只能放在类上 78 */ 79 80 @RequestMapping("testSessionAttributes") 81 public String testSessionAttributes(Map<String, Object> map) { 82 User user = new User(121,"tom","@qwqx.com"); 83 map.put("user", user); 84 map.put("email", "@myemail.com"); 85 return SUCCESS; 86 } 87 /**@RequestMapping("testModelAttribute") 88 * 如果数据库中的数据进行修改操作,默认的表单提交会new一个对象传回后台 89 * 如果规定某些属性无法修改,在表单里我们是不需要列出来的,而表单里我们不对属性进行赋值, 90 * 会造成对象属性为null,在写入数据库时造成麻烦, 91 * 当然我们可以用hidden赋值,但如果是私密属性又会有麻烦 92 * 在这里我们可以选择先从数据库插叙获取到对象,传送到页面,表单提交只是对属性值作出更改,而不是新建对象 93 */ 94 @RequestMapping("testModelAttribute") 95 public String testModelAttribute(@ModelAttribute("user")User user) { 96 System.out.println("修改:"+user); 97 return SUCCESS; 98 } 99 /** 100 * 由@ModelAttribute标记的方法会在每个目标方法执行之前被SpringMVC调用 101 * 在ModelAttribute修饰的方法中,放入到Map时的键需要和目标方法参数类型的第一个字母小写的字符串一致 102 * 可以在目标方法的参数上用@ModelAttribute("user") 用于指定获取对象时需要查找的键 103 * testModelAttribute(@ModelAttribute("user")User user) 104 */ 105 @ModelAttribute 106 public void getUser(@RequestParam(value="id",required=false) Integer id, 107 Map<String, Object> map) { 108 if(id!=null) { 109 // 假设user为数据库查询出来的对象 110 User user = new User(2,"Tom", "@tom.com"); 111 System.out.println("数据库查询出来的对象:"+user.toString()); 112 map.put("user",user); 113 } 114 } 115 @RequestMapping("testViewSourceAndViewResolver") 116 public String testViewSourceAndViewResolver() { 117 118 return SUCCESS; 119 } 120 @RequestMapping("testView") 121 public String testView() { 122 System.out.println("testView"); 123 return "helloView"; 124 } 125 @RequestMapping("testRedirect") 126 public String testRedirect() { 127 System.out.println("testRedirect"); 128 return "redirect:/index.jsp";//重定向 129 } 130 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <a href="helloword">Hello Word</a> 12 <br> 13 <form action="testUser"> 14 id:<input type="text" name="id"/> 15 <br> 16 userName:<input type="text" name="userName"/> 17 <br> 18 email:<input type="text" name="email"/> 19 <br> 20 provience:<input type="text" name="address.provience"/> 21 <br> 22 city:<input type="text" name="address.city"/> 23 <input type="submit" value="提交"/> 24 </form> 25 <br/> 26 <a href="testServletAPI">testServletAPI</a> 27 <br> 28 <a href="testModelAndView">testModelAndView</a> 29 <br> 30 <a href="testMap">test Map</a> 31 <br> 32 <a href="testSessionAttributes">testSessionAttributes</a> 33 34 <br><br> 35 <!-- 36 模拟修改操作 37 1.原始数据为 1 tom @tom.com 38 2.名字不能被修改 39 3.表单回显,模拟操作直接在表单填写对应的属性值 40 --> 41 <form action="testModelAttribute"> 42 <input type="hidden" name = "id" value="1"/> 43 email:<input type="text" name ="email" value="@tom.com"/> 44 <input type="submit" value="提交"/> 45 </form> 46 <br><br> 47 48 <a href="testViewSourceAndViewResolver">testViewSourceAndViewResolver</a> 49 50 <br><br> 51 国际化: 52 <br> 53 <fmt:message key="i18n.username"></fmt:message> 54 <br> 55 <fmt:message key="i18n.password"></fmt:message> 56 57 <br><br> 58 <a href="testView">testView</a> 59 <br><br> 60 <a href=testRedirect>testRedirect</a> 61 62 <br><br> 63 <a href="i18n?locale=zh_CH">中文</a> 64 <a href="i18n?locale=en_US">英文</a> 65 <br><br> 66 67 </body> 68 </html>
自定义视图
1 @Component 2 public class HelloView implements View{ 3 4 @Override 5 public String getContentType() { 6 return "text/html"; 7 } 8 9 @Override 10 public void render(Map<String, ?> arg0, HttpServletRequest arg1, HttpServletResponse arg2) throws Exception { 11 arg2.getWriter().print("hello view .time"+new Date()); 12 } 13 14 }
springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:task="http://www.springframework.org/schema/task" 7 xmlns:aop="http://www.springframework.org/schema/aop" 8 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 9 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd 10 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd 11 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 14 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 17 <!-- 配置自定义扫描得包 --> 18 <context:component-scan base-package="org.handler"></context:component-scan> 19 <!-- 配置视图解析器:如何把handler返回值解析为实际的物理视图 --> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 21 <property name="prefix" value="/WEB-INF/views/"></property> 22 <property name="suffix" value=".jsp"/> 23 </bean> 24 <!-- 自定义图 视图解析器 使用视图的名字来解析视图--> 25 <!-- 通过order属性来定义视图优先级 order值越小优先级越高--> 26 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> 27 <property name="order" value="100"></property> 28 </bean> 29 <!-- 配置直接转发的页面 --> 30 <mvc:view-controller path="/success" view-name="success"/> 31 <!-- 在实际开发中需要配置mvc:annotation-driven标签 --> 32 <mvc:annotation-driven></mvc:annotation-driven> 33 <!-- 配置国际化资源文件 --> 34 <bean id="messageSource" 35 class="org.springframework.context.support.ResourceBundleMessageSource"> 36 <property name="basename" value="i18n"></property> 37 </bean> 38 </beans>