标签: springmvc前端
[TOC]git
经过入门程序理解springmvc前端控制器、处理器映射器、处理器适配器、视图解析器用法。并附上入门程序的非注解的完整的配置文件,注解的完整配置文件。github
前端控制器配置:web
*.action
,访问以.action
结尾 由DispatcherServlet
进行解析/
,因此访问的地址都由DispatcherServlet
进行解析,对于静态文件的解析须要配置不让DispatcherServlet
进行解析,使用此种方式能够实现RESTful风格的url处理器映射器:spring
对标记@Controller
类中标识有@RequestMapping
的方法进行映射。在@RequestMapping
里边定义映射的url。使用注解的映射器不用在xml中配置url和Handler的映射关系。json
处理器适配器:spring-mvc
非注解处理器适配器(了解) 注解的处理器适配器(掌握) 注解处理器适配器和注解的处理器映射器是配对使用。理解为不能使用非注解映射器进行映射。mvc
<mvc:annotation-driven></mvc:annotation-driven>
能够代替下边的配置:app
<!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--注解适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
src/main/resources/springmvc.xml
jsp
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 配置Handler --> <bean name="/queryItems.action" class="com.iot.ssm.controller.ItemsController"/> <!-- 处理器映射器 将bean的name做为url进行查找,须要在配置Handler时指定beanname(就是url) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 处理器适配器 全部处理器适配器都实现了HandlerAdapter接口 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 视图解析器 解析jsp,默认使用jstl,classpath下要有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 对于注解的Handler 能够单个配置 实际开发中加你使用组件扫描 --> <!-- <bean class="com.iot.ssm.controller.ItemsController3"/> --> <!-- 能够扫描controller、service、... 这里让扫描controller,指定controller的包 --> <context:component-scan base-package="com.iot.ssm.controller"></context:component-scan> <!-- 注解的映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 注解的适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 使用mvc:annotation-driven代替上面两个注解映射器和注解适配的配置 mvc:annotation-driven默认加载不少的参数绑定方法, 好比json转换解析器默认加载了,若是使用mvc:annotation-driven则不用配置上面的RequestMappingHandlerMapping和RequestMappingHandlerAdapter 实际开发时使用mvc:annotation-driven --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 视图解析器 解析jsp,默认使用jstl,classpath下要有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路径的前缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置jsp路径的后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>