目录结构:

尚硅谷面试第一季-10SpringMVC的工作流程-LMLPHP

关键代码:

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 设置CharacterEncodingFilter -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置请求的字符集 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 设置响应的字符集 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 注册HiddenHttpMethodFilter,目的是为了将一个POST请求转换为PUT或者DELETE请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

SpringMVCHandler.java

 package SpringMVC的工作流程.handler;

 import java.util.Map;

 import SpringMVC的工作流程.entities.Department;
import SpringMVC的工作流程.entities.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class SpringMVCHandler { public static final String SUCCESS="success"; //1.简单的谈一下SpringMVC的工作流程 //处理模型数据方式一:将方法的返回值设置为ModelAndView
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
//1.创建ModelAndView对象
ModelAndView mav = new ModelAndView();
//2.设置模型数据,最终会放到request域中
mav.addObject("user", "admin");
//3.设置视图
mav.setViewName("success");
return mav;
}
/*
* ★处理模型数据方式二:方法的返回值仍是String类型,在方法的入参中传入Map、Model或者ModelMap
* 不管将处理器方法的返回值设置为ModelAndView还是在方法的入参中传入Map、Model或者ModelMap,
* SpringMVC都会转换为一个ModelAndView对象
*/
@RequestMapping("/testMap")
public String testMap(Map<String , Object> map) {
//向Map中添加模型数据,最终会自动放到request域中
map.put("user", new Employee(1, "韩总", "[email protected]", new Department(101, "教学部")));
return SUCCESS;
} }

运行结果:

尚硅谷面试第一季-10SpringMVC的工作流程-LMLPHP

尚硅谷面试第一季-10SpringMVC的工作流程-LMLPHP

尚硅谷面试第一季-10SpringMVC的工作流程-LMLPHP

尚硅谷面试第一季-10SpringMVC的工作流程-LMLPHP

完整代码:

https://gitee.com/ZhangShunHai/interview_atguigu_seasonone/tree/master/ssm_springmvc%20workflow

课堂重点:

SpringMVC工作流程图:

尚硅谷面试第一季-10SpringMVC的工作流程-LMLPHP

05-07 11:15