SpringMVC底层执行流程和原理是如何运作的?

2026-05-26 11:271阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计944个文字,预计阅读时间需要4分钟。

SpringMVC底层执行流程和原理是如何运作的?

一个简单的HelloSpringMVC程序+首先在web.xml中注册一个前端控制器(DispatcherServlet)+web-app+version=2.3+encoding=UTF-8+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_3_0.xsd

一个简单的HelloSpringMVC程序

先在web,xml中注册一个前端控制器(DispatcherServlet)

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置DispatcherServlet:这是SpringMVC的核心,请求分发器,前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--要绑定springmvc的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别为1--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

配置文件(springmvc-servlet.xml)

HandlerMapper是处理器映射器-->根据请求的地址去找处理器(如案例中的"/hello")

HandlerAdapter是处理器适配器-->找到处理器后根据id去适配对应的controller(如适配到案例中的HelloController),controller会返回ModelAndView及其前端数据

ViewResolver是视图解析器,其作用为:

1.获取到ModelAndView中的数据

2.解析视图名称

3.拼接视图名称

4.数据渲染

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="/hello" class="com.kuang.contorller.HelloController"/> </beans>

controller层:

实现Controller接口,重写内部方法(一般不会使用,这是底层原理)

ModelAndView是模型、视图

public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

在springmvc-servlet.xml中配置视图解析器等

<context:component-scan base-package="com.kuang.controller"/> /*组件扫描,用于扫描controller下的包*/ <mvc:default-servlet-handler/> /*静态资源过滤*/ <mvc:annotation-driven/> /*这个就帮助我们配置了映射器以及适配器*/

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.kuang.controller"/> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

contorller

@Controller    //说明这类被Spring托管了 @RequestMapping("/hello") public class HelloController { @RequestMapping("/h1")   //这个注解会执行视图解析器 public String hello(Model model){ model.addAttribute("msg","helloSpringMVCAnnotation"); return "hello"; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

SpringMVC底层执行流程和原理是如何运作的?

本文共计944个文字,预计阅读时间需要4分钟。

SpringMVC底层执行流程和原理是如何运作的?

一个简单的HelloSpringMVC程序+首先在web.xml中注册一个前端控制器(DispatcherServlet)+web-app+version=2.3+encoding=UTF-8+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_3_0.xsd

一个简单的HelloSpringMVC程序

先在web,xml中注册一个前端控制器(DispatcherServlet)

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置DispatcherServlet:这是SpringMVC的核心,请求分发器,前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--要绑定springmvc的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别为1--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

配置文件(springmvc-servlet.xml)

HandlerMapper是处理器映射器-->根据请求的地址去找处理器(如案例中的"/hello")

HandlerAdapter是处理器适配器-->找到处理器后根据id去适配对应的controller(如适配到案例中的HelloController),controller会返回ModelAndView及其前端数据

ViewResolver是视图解析器,其作用为:

1.获取到ModelAndView中的数据

2.解析视图名称

3.拼接视图名称

4.数据渲染

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="/hello" class="com.kuang.contorller.HelloController"/> </beans>

controller层:

实现Controller接口,重写内部方法(一般不会使用,这是底层原理)

ModelAndView是模型、视图

public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

在springmvc-servlet.xml中配置视图解析器等

<context:component-scan base-package="com.kuang.controller"/> /*组件扫描,用于扫描controller下的包*/ <mvc:default-servlet-handler/> /*静态资源过滤*/ <mvc:annotation-driven/> /*这个就帮助我们配置了映射器以及适配器*/

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.kuang.controller"/> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

contorller

@Controller    //说明这类被Spring托管了 @RequestMapping("/hello") public class HelloController { @RequestMapping("/h1")   //这个注解会执行视图解析器 public String hello(Model model){ model.addAttribute("msg","helloSpringMVCAnnotation"); return "hello"; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

SpringMVC底层执行流程和原理是如何运作的?