杭州市区网站制作单位,青海公路建设服务网站,婚纱摄影网站排名,淘宝推广一、Spring MVC处理流程1.Spring MVC将所有请求都交由DispatchServlet进行处理。2.DispatchServlet获取HandlerMapping(处理映射器)#xff0c;然后找到对应的HandlerBean处理Controller请求#xff0c;并返回一个ModelAndView对象。3.DispatchServlet查询一个或多个ViewReso…一、Spring MVC处理流程1.Spring MVC将所有请求都交由DispatchServlet进行处理。2.DispatchServlet获取HandlerMapping(处理映射器)然后找到对应的HandlerBean处理Controller请求并返回一个ModelAndView对象。3.DispatchServlet查询一个或多个ViewResolver视图解析器对象 并把视图渲染返回给前端。二、相关配置首先配置web.xml,我们根据组件启动顺序全局参数、Listener监听器、Filter过滤器、Servlet、的顺序配置。xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsdcontextConfigLocationclasspath*:applicationContext.xmlorg.springframework.web.context.ContextLoaderListeneropenSessionInterceptororg.springframework.orm.hibernate5.support.OpenSessionInViewFilteropenSessionInterceptor/*encodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8encodingFilter/*contextorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath*:/spring-mvc.xmlcontext/我们首先配置了org.springframework.web.context.ContextLoaderListener的Context监听器我们以Tomcat为例1、他启动的时候会去读取配置文件web.xml首先读两个节点: 和 然后创建一个ServletContext,整个web项目将共享这个Context。(意味着我们这个项目将以spring MVC的context方式启动)2.容器将的内容以键值对的形式交给Listener,配置中我们配置了contextConfigLocation也就是springmvc配置文件的位置作为启动springContext的配置文件这个值在容器启动的时候监听器执行contextInitialized方法可以拿到然后 sc.getInitParameter(CONFIG_LOCATION_PARAM)获得配置文件路径再根据配置文件路径初始化容器。(Spring源码如下)1 String configLocationParam sc.getInitParameter(CONFIG_LOCATION_PARAM);2 if (configLocationParam ! null) {3 wac.setConfigLocation(configLocationParam);4 }其中ConfigLocationPraram是常量1 public static final String CONFIG_LOCATION_PARAM contextConfigLocation;3.接下来是两个过滤器分别是openSessionInterceptor为了给每个请求绑定HibernateSession的在GetCurrentSession的时候可以由Spring进行统一管理无需手动干扰另外一个是字符集过滤器将请求的编码统一。4.最后是DispatchServlet调度器他是SpringMVC的核心他拦截了所有请求并将请求统一管理。注意拦截路径必须写成/不能写成/*因为“/*”意为拦截所有请求只要是请求一律拦截而“/”意为将DispatcherServlet作为default Servlet(默认是org.apache.catalina.servlets.DefaultServlet)所有其他路径映射未匹配情况下才会交由它处理。而由于隐式映射的关系使得 .jsp 扩展名被映射到静态资源进而被执行。例如配置成“/*”执行过程中遇到的配置成“/*”他将拦截所有请求当返回视图路径资源时请求资源的请求被当成了servlet给拦截了进而想要执行对应的Controller发现没有对应的视图但是根本不存在导致404错误。接下来配置springMVC配置文件xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/context xmlns:mvchttp://www.springframework.org/schema/toolxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd这是spring mvc配置文件因为我们使用注解方式开发所以要使用扫描包扫描注解其实默认就是扫描注解的所以可以直接写成第二个是视图解析器他将配合控制器使用控制器代码如下Controllerpublic class LoginController{Resourceprivate UserService userService;RequestMapping(/index.do)public String index(){return index;}}Controller注解说明这是一个控制器Bean他会被HandlerMapping管理到RequestMapping注解声明了servlet的访问路径里面有多个属性value:指定请求的实际地址method 指定请求方式GET、POST、PUT、DELETE等(默认Get请求)consumes 指定处理请求的提交内容类型(Content-Type)例如application/json, text/html;produces: 指定返回的内容类型仅当request请求头中的(Accept)类型中包含该指定类型才返回params 定request中必须包含某些参数值是才让该方法处理。headers 指定request中必须包含某些指定的header值才能让该方法处理请求。最后返回的“index字符串将会和视图解析器的前缀和后缀进行拼接形成新的路径”即/WEB-INF/pages/index.jsp;请求的时候只要http://地址端口/项目名/实际的RequestMapping名称即可如http:localhost:8080/springDemo/index.do