springMvc的执行流程介绍
一、大体的步骤是?
1) 用户发送请求
2)前端控制器获得用户请求的URL,发送URL给处理器映射
3)处理器映射将Handler(包装方法信息)返回给前端控制器
4)前端控制器发送Handler给处理器适配器,适配器执行Handler方法
5)执行Handler方法后,返回ModelAndView(逻辑视图)给前端控制器
6)前端控制器将ModelAndView发送给视图解析器,解析出物理视图返回给前端控制器
7)前端控制器渲染视图,发送视图给用户
二、 通过下面的例子来理解会更好
首先要配置web.xml和springmvc
//springmvc.xml配置
@Controller
public class HelloController {
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 包扫描-->
<context:component-scan base-package="com.lg.mvc"></context:component-scan>
<!--视图解析器-->
<bean id="viewResolverByLG" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 找到/WEB-INF/pages/index.jsp-->
<!-- 配置前缀-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--处理动态资源-->
<mvc:default-servlet-handler/>
<!--启动注解配置-->
<mvc:annotation-driven/>
</beans>
//web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数-->
<init-param>
<!-- 配置spring-mvc配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 配置启动顺序-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--处理所有资源-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
//测试类配置
@Controller
public class HelloController {
/*
* 请求映射 http://localhost:8080/goes---> /WEB-INF/pages/index.jsp
* */
@RequestMapping("goes")
public String indexByLg(){
return "index";
}
}
//网页配置
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
MVC流程的介绍哦
//测试类配置
@Controller
public class HelloController {
/*
* 请求映射 http://localhost:8080/goes---> /WEB-INF/pages/index.jsp
* */
@RequestMapping("goes")
public String indexByLg(){
return "index";
}
}
2.部分解释
Handler(包装方法信息)和RequestMapping:springMVC通过RequestMapping注释的关键字来获取到他的方法即Handler(包装方法信息),RequestMapping的命名随意,也可以在他的类上设置一个,但是return的值必须对应:
ModelAndView(逻辑视图):逻辑视图就是我们在Handler(包装方法信息)中获取到的字符串元素
物理视图:通过视图解析器解析出内容的具体位置的就是物理视图,解析实际上就是将字符串拼接了,把前缀和后缀的中间加上了返回值,而且虽然获得了该地址,但给用户的还是原网址不变
效果图。