通過入門程序理解springmvc前端控制器、處理器映射器、處理器適配器、視圖解析器用法。並附上入門程序的非注解的完整的配置文件,注解的完整配置文件。
前端控制器配置:
第一種:*.action
,訪問以.action
結尾 由DispatcherServlet
進行解析 第二種:/
,所以訪問的地址都由DispatcherServlet
進行解析,對於靜態文件的解析需要配置不讓DispatcherServlet
進行解析,使用此種方式可以實現RESTful風格的url
處理器映射器:
非注解處理器映射器(了解) 注解的處理器映射器(掌握)對標記@Controller
類中標識有@RequestMapping
的方法進行映射。在@RequestMapping
裡邊定義映射的url。使用注解的映射器不用在xml中配置url和Handler的映射關系。
處理器適配器:
非注解處理器適配器(了解)
注解的處理器適配器(掌握)
注解處理器適配器和注解的處理器映射器是配對使用。理解為不能使用非注解映射器進行映射。
可以代替下邊的配置:
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%E6%B3%A8%E8%A7%A3%E6%98%A0%E5%B0%84%E5%99%A8%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <!--{cke_protected}{C}%3C!%2D%2D%E6%B3%A8%E8%A7%A3%E9%80%82%E9%85%8D%E5%99%A8%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> </bean></bean></code>
src/main/resources/springmvc.xml
<code class="language-xml hljs "><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"> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEHandler%20%2D%2D%3E--> <bean name="/queryItems.action" class="com.iot.ssm.controller.ItemsController"> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%A4%84%E7%90%86%E5%99%A8%E6%98%A0%E5%B0%84%E5%99%A8%0A%20%20%20%20%E5%B0%86bean%E7%9A%84name%E4%BD%9C%E4%B8%BAurl%E8%BF%9B%E8%A1%8C%E6%9F%A5%E6%89%BE%EF%BC%8C%E9%9C%80%E8%A6%81%E5%9C%A8%E9%85%8D%E7%BD%AEHandler%E6%97%B6%E6%8C%87%E5%AE%9Abeanname(%E5%B0%B1%E6%98%AFurl)%0A%20%20%20%20%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%A4%84%E7%90%86%E5%99%A8%E9%80%82%E9%85%8D%E5%99%A8%0A%20%20%20%20%20%E6%89%80%E6%9C%89%E5%A4%84%E7%90%86%E5%99%A8%E9%80%82%E9%85%8D%E5%99%A8%E9%83%BD%E5%AE%9E%E7%8E%B0%E4%BA%86HandlerAdapter%E6%8E%A5%E5%8F%A3%0A%20%20%20%20%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"> <!--{cke_protected}{C}%3C!%2D%2D%20%E8%A7%86%E5%9B%BE%E8%A7%A3%E6%9E%90%E5%99%A8%0A%20%20%20%20%E8%A7%A3%E6%9E%90jsp%2C%E9%BB%98%E8%AE%A4%E4%BD%BF%E7%94%A8jstl%2Cclasspath%E4%B8%8B%E8%A6%81%E6%9C%89jstl%E7%9A%84%E5%8C%85%0A%20%20%20%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean></bean></bean></bean></beans></code>
<code class="language-xml hljs "><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"> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%AF%B9%E4%BA%8E%E6%B3%A8%E8%A7%A3%E7%9A%84Handler%20%E5%8F%AF%E4%BB%A5%E5%8D%95%E4%B8%AA%E9%85%8D%E7%BD%AE%0A%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E4%B8%AD%E5%8A%A0%E4%BD%A0%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6%E6%89%AB%E6%8F%8F%0A%20%20%20%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%20%3Cbean%20%20class%3D%22com.iot.ssm.controller.ItemsController3%22%2F%3E%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8F%AF%E4%BB%A5%E6%89%AB%E6%8F%8Fcontroller%E3%80%81service%E3%80%81...%0A%20%20%20%20%E8%BF%99%E9%87%8C%E8%AE%A9%E6%89%AB%E6%8F%8Fcontroller%EF%BC%8C%E6%8C%87%E5%AE%9Acontroller%E7%9A%84%E5%8C%85%0A%20%20%20%20%20%2D%2D%3E--> <context:component-scan base-package="com.iot.ssm.controller"></context:component-scan> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E7%9A%84%E6%98%A0%E5%B0%84%E5%99%A8%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E7%9A%84%E9%80%82%E9%85%8D%E5%99%A8%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E4%BB%A3%E6%9B%BF%E4%B8%8A%E9%9D%A2%E4%B8%A4%E4%B8%AA%E6%B3%A8%E8%A7%A3%E6%98%A0%E5%B0%84%E5%99%A8%E5%92%8C%E6%B3%A8%E8%A7%A3%E9%80%82%E9%85%8D%E7%9A%84%E9%85%8D%E7%BD%AE%0A%20%20%20%20%20mvc%3Aannotation-driven%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E5%BE%88%E5%A4%9A%E7%9A%84%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A%E6%96%B9%E6%B3%95%EF%BC%8C%0A%20%20%20%20%20%E6%AF%94%E5%A6%82json%E8%BD%AC%E6%8D%A2%E8%A7%A3%E6%9E%90%E5%99%A8%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E4%BA%86%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E5%88%99%E4%B8%8D%E7%94%A8%E9%85%8D%E7%BD%AE%E4%B8%8A%E9%9D%A2%E7%9A%84RequestMappingHandlerMapping%E5%92%8CRequestMappingHandlerAdapter%0A%20%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E6%97%B6%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%0A%20%20%20%20%20%2D%2D%3E--> <mvc:annotation-driven></mvc:annotation-driven> <!--{cke_protected}{C}%3C!%2D%2D%20%E8%A7%86%E5%9B%BE%E8%A7%A3%E6%9E%90%E5%99%A8%0A%20%20%20%20%E8%A7%A3%E6%9E%90jsp%2C%E9%BB%98%E8%AE%A4%E4%BD%BF%E7%94%A8jstl%2Cclasspath%E4%B8%8B%E8%A6%81%E6%9C%89jstl%E7%9A%84%E5%8C%85%0A%20%20%20%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEjsp%E8%B7%AF%E5%BE%84%E7%9A%84%E5%89%8D%E7%BC%80%20%2D%2D%3E--> <property name="prefix" value="/WEB-INF/jsp/"> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEjsp%E8%B7%AF%E5%BE%84%E7%9A%84%E5%90%8E%E7%BC%80%20%2D%2D%3E--> <property name="suffix" value=".jsp"> </property></property></bean> </bean></bean></beans></code>