目录结构:
关键代码:
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <welcome-file-list> 7 <welcome-file>index.jsp</welcome-file> 8 </welcome-file-list> 9 <!-- 设置CharacterEncodingFilter --> 10 <filter> 11 <filter-name>CharacterEncodingFilter</filter-name> 12 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 13 <!-- 设置请求的字符集 --> 14 <init-param> 15 <param-name>encoding</param-name> 16 <param-value>UTF-8</param-value> 17 </init-param> 18 <!-- 设置响应的字符集 --> 19 <init-param> 20 <param-name>forceEncoding</param-name> 21 <param-value>true</param-value> 22 </init-param> 23 </filter> 24 <filter-mapping> 25 <filter-name>CharacterEncodingFilter</filter-name> 26 <url-pattern>/*</url-pattern> 27 </filter-mapping> 28 <servlet> 29 <servlet-name>springDispatcherServlet</servlet-name> 30 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 31 <init-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>WEB-INF/springmvc.xml</param-value> 34 </init-param> 35 <load-on-startup>1</load-on-startup> 36 </servlet> 37 38 <servlet-mapping> 39 <servlet-name>springDispatcherServlet</servlet-name> 40 <url-pattern>/</url-pattern> 41 </servlet-mapping> 42 43 <!-- 注册HiddenHttpMethodFilter,目的是为了将一个POST请求转换为PUT或者DELETE请求 --> 44 <filter> 45 <filter-name>HiddenHttpMethodFilter</filter-name> 46 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 47 </filter> 48 <filter-mapping> 49 <filter-name>HiddenHttpMethodFilter</filter-name> 50 <url-pattern>/*</url-pattern> 51 </filter-mapping> 52 </web-app>
SpringMVCHandler.java
1 package SpringMVC的工作流程.handler; 2 3 import java.util.Map; 4 5 import SpringMVC的工作流程.entities.Department; 6 import SpringMVC的工作流程.entities.Employee; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.servlet.ModelAndView; 10 11 12 13 @Controller 14 public class SpringMVCHandler { 15 16 public static final String SUCCESS="success"; 17 18 //1.简单的谈一下SpringMVC的工作流程 19 20 //处理模型数据方式一:将方法的返回值设置为ModelAndView 21 @RequestMapping("/testModelAndView") 22 public ModelAndView testModelAndView() { 23 //1.创建ModelAndView对象 24 ModelAndView mav = new ModelAndView(); 25 //2.设置模型数据,最终会放到request域中 26 mav.addObject("user", "admin"); 27 //3.设置视图 28 mav.setViewName("success"); 29 return mav; 30 } 31 /* 32 * ★处理模型数据方式二:方法的返回值仍是String类型,在方法的入参中传入Map、Model或者ModelMap 33 * 不管将处理器方法的返回值设置为ModelAndView还是在方法的入参中传入Map、Model或者ModelMap, 34 * SpringMVC都会转换为一个ModelAndView对象 35 */ 36 @RequestMapping("/testMap") 37 public String testMap(Map<String , Object> map) { 38 //向Map中添加模型数据,最终会自动放到request域中 39 map.put("user", new Employee(1, "韩总", "hybing@atguigu.com", new Department(101, "教学部"))); 40 return SUCCESS; 41 } 42 43 }
运行结果:
完整代码:
https://gitee.com/ZhangShunHai/interview_atguigu_seasonone/tree/master/ssm_springmvc%20workflow
课堂重点:
SpringMVC工作流程图: