一、执行机制
下图是比较全面的过程
1、用户发送请求到前端控制器DispatcherServlet
2、DispatcherServlet对请求进行拦截并收到url和请求方法,然后调用处理映射器HandlerMapping(其中为Map存放机制,通过key(url和方法)来找value(类名、方法名、参数列表))
3、处理映射器根据请求url找到具体的处理器,生成处理器执行链HandlerExecutionChain(包含处理器对象和处理器拦截器)返回给DispatcherServlet
4、DispatcherServlet根据处理器Handler获取对应的适配器
5、HandlerAdapter调用处理器Handler
6、Handler(Controller)执行完成后把方法的返回值返回ModelAndView
7、HandlerAdapter返回ModelAndView
8、DispatcherServlet统一将返回的ModelAndView派送到ViewResolve(视图解析器)解析
9,视图解析器解析(View resolver)之后返回View
10、对View进行渲染
11、响应用户
注意:如果继续拓展的话适配器会先找到controller类然后去调用service接口之后再调用dao层数据库,然后加载数据库的信息,最后如序返回
如果其中用到转json格式(前后端分离)需要在controller层先用fastjson吧数据格式转为json在返回给DispatcherServlet
如果其中包含请求头的话需要在传给controller时加上请求头。
其中用到了比较多的注解后面会单独发布一篇给大家说明注解的原理和作用
二、环境搭建
1、首先需要导入需要的包spring-webmvche servler包
2、在web.xml中配置DispatcherServlet,web.xml位置一定放对,如果是maven项目需要自己创建,下面是我的模板
<?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"> <filter>配置字符编码过滤器 <filter-name>charsetfilter</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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>charsetfilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> <servlet> <servlet-name>mvc</servlet-name>//和mapping中的名字一致 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name>//如果不加namespace就会对应属性-servlet.xml,加上的话就可以自己起名字如下 <param-value>springmvc</param-value> </init-param> <load-on-startup>1</load-on-startup>//这样可以在启动的时候加载控制器,一定卸载servlet的最后 </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern>//路径匹配,也就是拦截一下url但是不拦截jsp/就是匹配路径后面有效的,/*是匹配后面所有的,在这里就会一直循环 </servlet-mapping> </web-app>
3、在springmvc,xml中配置文件
首先在webapp/WEB-INF目录下创建spring的xml文件,格式可以提前自己定义好,下面是我的spring.xml模板
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd//这一行可能会报错但是不影响执行,去掉会有警告 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd "> <mvc:annotation-driven>开启注解驱动 <mvc:message-converters register-defaults="false"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>//让注解生效 </mvc:message-converters> </mvc:annotation-driven> <context:component-scan base-package="com.neuedu.controller"></context:component-scan>//可以扫描这个包下的所有带@controller注解的类 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>//配置视图解析器 <property name="prefix" value="/WEB-INF/pages/"></property>前缀 <property name="suffix" value=".jsp"></property>后缀 </bean> </beans>
4、写具体实现代码
下面演示一个例子来测试一下
首先写一个实体类
@Data//lombok插件的注解,代表setget方法 @AllArgsConstructor//有参构造 @NoArgsConstructor//无参构造 public class User { private Integer id; private String username; private String likes; }
然后在controller类中定义一个user类
类上一定加@Controller注解,其中的方法为
UserService userService = new UserServiceImpl();//父类引用指向子类对象,接口引用指向实现类对象 @GetMapping("/list")//url信息 String list(ModelMap modelMap) {//ModelMap可以将参数放进作用域,也可以用ModelAndVew // 读数据 放进作用域 modelMap.addAttribute("users",userService.list());//这里直接调用的逻辑层返回的user列表 return "list";//转发到list.jsp }
这样就可以带着数据去视图层进行渲染了。
注意:如果是前后端分离项目需要在类上在家一个注解@ResponseBody,返回值转成json类型直接return即可。