標簽: springmvc
通過入門程序理解springmvc前端控制器、處理器映射器、處理器適配器、視圖解析器用法。並附上入門程序的非注解的完整的配置文件,注解的完整配置文件。
前端控制器配置:
*.action
,訪問以.action
結尾 由DispatcherServlet
進行解析/
,所以訪問的地址都由DispatcherServlet
進行解析,對於靜態文件的解析需要配置不讓DispatcherServlet
進行解析,使用此種方式可以實現RESTful風格的url處理器映射器:
對標記@Controller
類中標識有@RequestMapping
的方法進行映射。在@RequestMapping
里邊定義映射的url。使用注解的映射器不用在xml中配置url和Handler的映射關系。
處理器適配器:
非注解處理器適配器(了解)
注解的處理器適配器(掌握)
注解處理器適配器和注解的處理器映射器是配對使用。理解為不能使用非注解映射器進行映射。
<mvc:annotation-driven></mvc:annotation-driven>
可以代替下邊的配置:
<!--注解映射器 -->
<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
<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>
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。