SpringMVC底層執(zhí)行流程及原理解析
一個(gè)簡單的HelloSpringMVC程序
先在web,xml中注冊一個(gè)前端控制器(DispatcherServlet)
<?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"> <!--配置DispatcherServlet:這是SpringMVC的核心,請求分發(fā)器,前端控制器--> <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> <!--啟動(dòng)級別為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是處理器映射器-->根據(jù)請求的地址去找處理器(如案例中的"/hello")
HandlerAdapter是處理器適配器-->找到處理器后根據(jù)id去適配對應(yīng)的controller(如適配到案例中的HelloController),controller會(huì)返回ModelAndView及其前端數(shù)據(jù)
ViewResolver是視圖解析器,其作用為:
1.獲取到ModelAndView中的數(shù)據(jù)
2.解析視圖名稱
3.拼接視圖名稱
4.數(shù)據(jù)渲染
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://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層:
實(shí)現(xiàn)Controller接口,重寫內(nèi)部方法(一般不會(huì)使用,這是底層原理)
ModelAndView是模型、視圖
public class HelloController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("msg","HelloSpringMVC!"); mv.setViewName("test"); return mv; } }
底層流程圖
實(shí)線是SpringMVC已經(jīng)幫你實(shí)現(xiàn)好了,虛線是需要自己手動(dòng)
以上僅是說明底層執(zhí)行原理,實(shí)際開發(fā)并不會(huì)這樣去使用!
在實(shí)際開發(fā)中SpringMVC推薦使用注解的方式
在注解開發(fā)中,不需要我們?nèi)ヅ渲锰幚砥鬟m配器和處理器映射器。
web.xml中只需配置DispatcherServlet前端控制器
<?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"> <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/> /*靜態(tài)資源過濾*/ <mvc:annotation-driven/> /*這個(gè)就幫助我們配置了映射器以及適配器*/
<?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.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") //這個(gè)注解會(huì)執(zhí)行視圖解析器 public String hello(Model model){ model.addAttribute("msg","helloSpringMVCAnnotation"); return "hello"; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在logback.xml中自定義動(dòng)態(tài)屬性的方法
這篇文章主要介紹了在logback.xml中自定義動(dòng)態(tài)屬性的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08mac下修改idea的jvm運(yùn)行參數(shù)解決idea卡頓的情況
這篇文章主要介紹了mac下修改idea的jvm運(yùn)行參數(shù)解決idea卡頓的情況,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12使用spring oauth2框架獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用spring oauth2框架獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Mybatis collection查詢集合屬性報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Mybatis collection查詢集合屬性報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09從零搭建Spring Boot腳手架整合OSS作為文件服務(wù)器的詳細(xì)教程
這篇文章主要介紹了從零搭建Spring Boot腳手架整合OSS作為文件服務(wù)器的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08