springmvc的一个特色:本质是控制器。java
1.HandlerMapping:根据请求的url找到 处理器的方法。web
maven dependencies/spring-webmvc/../DispatcherServlet.properties默认的处理器映射器已过期,项目中不推荐使用。spring
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\ org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
配置:resources/springmvc.xml浏览器
选中“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”ctrl+shift+t快速进入对应类,读目标类的对应注解和注释。spring-mvc
<?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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置包扫描controller --> <context:component-scan base-package="com.buff.controller"></context:component-scan> <!-- 配置处理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> </beans>
2.HandlerAdapter:执行处理器方法。tomcat
默认的处理器适配器已过期,项目中不推荐使用。mvc
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\ org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\ org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
<!-- 配置处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
映射器和适配器配套使用,要么都不配置,不然http status 500.app
3.注解驱动方式配置 处理器映射器 和 处理器适配器jsp
<!-- 说明: 1.等于同时配置了RequestMappingHandlerMapping/RequestMappingHandlerAdapter 2.应用项目中的使用方式 --> <mvc:annotation-driven></mvc:annotation-driven>
4.ViewResolver:把逻辑视图(在处理器方法中设置的视图路径)解析成物理视图(在浏览器实际看到的页面)。maven
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
<!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置视图的公共的目录路径 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 配置视图的扩展名称 --> <property name="suffix" value=".jsp"></property> </bean>
*公共资源可抽取出进行配置。
*更改代码后,从新启动tomcat前要点击console->terminate