SpringMvc框架结构图

springMvc44-springMVC的三大组件_mvc

  • 处理器映射器:用户请求路径到Controller方法的映射
  • 处理器适配器:根据handler(controlelr类)的开发方式(注解开发/其他开发) 方式的不同区寻找不同的处理器适配器
  • 视图解析器:根据handler返回的view地址文件类型(jsp/pdf….)去寻找相应的视图解析器来进行解析
    SpringMvc框架配置要点1:应该在SpirngMvc的核心配置文件中配置处理器映射器和处理器适配器,否则SpringMvc就会去/org/springframework/web/servlet/DispatcherServlet.properties这个文件中去依次去找处理器映射器和处理器适配器,这样每一次的请求都会去依次判断,这样的效率会很慢的。
​​<!-- 注解形式的处理器映射器 打开源码发现已经过时 -->​​
​​<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->​​
​​<!-- 注解形式的处理器适配器 打开源码发现已经过时-->​​
​​<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->​​
​​<!-- 配置最新版的注解的处理器映射器 -->​​
​​<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->​​
​​<!-- 配置最新版的注解的处理器适配器 -->​​
​​<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> ​​

但是上面的配置最新版的注解处理器映射器和处理器适配器仍然不好,如果官方后续版本升级后续仍然会升级方法,这个类仍然或有可能过时,此时在项目中再修改就有些麻烦,此时我们可以只需要配置一个注解驱动就行了

​​<!-- 注解驱动:​​
​​作用:替我们自动配置最新版的注解的处理器映射器和处理器适配器​​
​​-->​​
​​<mvc:annotation-driven></mvc:annotation-driven>​​

SpringMvc配置视图解析器可配可不配

​​<!-- 配置视图解析器 ​​
​​作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称​​
​​-->​​
​​<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">​​
​​<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->​​
​​<!-- 前缀 -->​​
​​<property name="prefix" value="/WEB-INF/jsp/"></property>​​
​​<!-- 后缀 -->​​
​​<property name="suffix" value=".jsp"></property>​​
​​</bean>​​

下面给出完整的SpringMvc的核心配置文件

​​<?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:dubbo="http:///schema/dubbo" ​​
​​xmlns:mvc="http://www.springframework.org/schema/mvc"​​
​​xsi:schemaLocation="http://www.springframework.org/schema/beans ​​
​​http://www.springframework.org/schema/beans/spring-beans-4.0.xsd​​
​​http://www.springframework.org/schema/mvc ​​
​​http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd​​
​​http:///schema/dubbo ​​
​​http:///schema/dubbo/dubbo.xsd​​
​​http://www.springframework.org/schema/context ​​
​​http://www.springframework.org/schema/context/spring-context-4.0.xsd">​​

​​<!-- 配置@Controller注解扫描 -->​​
​​<context:component-scan base-package="cn.itheima.controller"></context:component-scan>​​


​​<!-- 注解驱动:​​
​​作用:替我们自动配置最新版的注解的处理器映射器和处理器适配器​​
​​-->​​
​​<mvc:annotation-driven></mvc:annotation-driven>​​


​​<!-- 配置视图解析器 ​​
​​作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称​​
​​-->​​
​​<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">​​
​​<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->​​
​​<!-- 前缀 -->​​
​​<property name="prefix" value="/WEB-INF/jsp/"></property>​​
​​<!-- 后缀 -->​​
​​<property name="suffix" value=".jsp"></property>​​
​​</bean>​​

​​</beans>​​

杂家不如专家,精益求精