SpringMVC底层执行流程及原理解析

网友投稿 245 2022-12-06


SpringMVC底层执行流程及原理解析

一个简单的HelloSpringMVC程序

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

xmlns:xsi="http://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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

springmvc

/

xmlns:xsi="http://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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

springmvc

/

配置文件(springmvc-servlet.xml)

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

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

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

1.获取到ModelAndView中的数据

2.解析视图名称

3.拼接视图名称

4.数据渲染

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

controller层:

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

ModelAndView是模型、视图

public class HelloController implements Controller {

@Override

public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

ModelAndView mv = new ModelAndView();

mv.addObject("msg","HelloSpringMhttp://VC!");

mv.setViewName("test");

return mv;

}

}

底层流程图

实线是SpringMVC已经帮你实现好了,虚线是需要自己手动

以上仅是说明底层执行原理,实际开发并不会这样去使用!

在实际开发中SpringMVC推荐使用注解的方式

在注解开发中,不需要我们去配置处理器适配器和处理器映射器。

web.xml中只需配置DispatcherServlet前端控制器

xmlns:xsi="http://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">

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

SpringMVC

/

xmlns:xsi="http://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">

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

SpringMVC

/

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

/*组件扫描,用于扫描controller下的包*/

/*静态资源过滤*/

/*这个就帮助我们配置了映射器以及适配器*/

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springfrZXMgjPPqzamework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

https://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springfrZXMgjPPqzamework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

https://springframework.org/schema/mvc/spring-mvc.xsd">

contorller

@Controller    //说明这类被Spring托管了

@RequestMapping("/hello")

public class HelloController {

@RequestMapping("/h1")   //这个注解会执行视图解析器

public String hello(Model model){

model.addAttribute("msg","helloSpringMVCAnnotation");

return "hello";

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:教你在 IntelliJ IDEA 中使用 VIM插件的详细教程
下一篇:JAVA 多态操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~