標籤: 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>