zoukankan      html  css  js  c++  java
  • spring mvc 基于注解 配置默认 handlermapping

    spring mvc 是类似于 Struts 的框架。他们都有一个最主要的功能就是URL路由。URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 controller )映射起来。抛开其他的功能,spring mvc 要比 Struts 在URL路由功能上灵活很多。比如要实现 RESTful,如果用 Struts ,需要安装 一些插件,而且插件也常常限制的很死。但是如果用 spring mvc ,那就驾轻就熟。
    下面具体看一下spring mvc
    在 spring 2.5 以后,可以利用注解写进行路由映射,简单,直观。
    配置 web.xml

    Xml代码 收藏代码

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    5. <!-- Processes application requests -->
    6. <servlet>
    7. <servlet-name>appServlet</servlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocation</param-name>
    11. <param-value>classpath:spring-servlet-context.xml</param-value>
    12. </init-param>
    13. <load-on-startup>0</load-on-startup>
    14. </servlet>
    15. <servlet-mapping>
    16. <servlet-name>appServlet</servlet-name>
    17. <url-pattern>/</url-pattern>
    18. </servlet-mapping>
    19. </web-app>

    spring 的配置文件 spring-servlet-context.xml

    Xml代码 收藏代码

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:mvc="http://www.springframework.org/schema/mvc"
    5. xsi:schemaLocation=" 
    6.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    8. <context:component-scan base-package="com.myPackage"/>
    9. <mvc:annotation-driven/>
    10. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    11. <property name="prefix" value="/WEB-INF/pages/" />
    12. <property name="suffix" value=".jsp" />
    13. </bean>
    14. </beans>

    Java代码 收藏代码

    1. package com.myPackage.controller; 
    2. @Controller
    3. public class FooController{ 
    4. /**
    5. * 响应 /foo 请求,并返回 bar.jsp 做为渲染页面 (不一定非是jsp,只不过以上配置的是jsp)
    6. */
    7. @RequestMappting("/foo") 
    8. public String foo(){ 
    9. //process logic
    10. return "bar";  

    以上即 spring mvc 一个简单的配置。mvc:annotation-driven 即告诉 spring mvc 使用注解的方式进行URL路由配置。controller 类上加上 @Controller 告诉 spring mvc 此类是 controller,同时方法上的 @RequestMapping 注解告诉 spring mvc 这里有一个请求映射到这个方法。
    前面都是最基本的配置,但有一个缺点就是还不能响应静态页面的请求。 比如 webroot 下有 一个 bar.html 页面,但 webroot/bar.html 是请求不到 bar.html 的。因为目前为止,还没有对 bar.html 的路由映射。
    要能请求静态页面,或者资源(如css,js,图片等),需要做如下配置

    Xml代码 收藏代码

    1. <mvc:resources mapping="/**" location="/" />

    但是上面的配置还是有问题的,如果有一个 foo.html 的页面,请求是 webroot/foo.html,但是会发现会被路由到 /foo.* ,然后程序交给了 foo() 方法,没能得到 foo.html 页面。原因是使用 mvc:annotation-driven 做配置,spring mvc 会默认使用 DefaultAnnoationHandlerMapping 和 AnnotationMethodHandlerAdapter ,而 DefaultAnnotationHandlerMapping 会默认对 @RequestMapping 上的路由做三个映射,如 @RequestMapping("/foo") 会被做 /foo 、/foo.* 和 /foo/ 这样三个映射。mvc:resources 默认使用 SimpleUrlHandlerMapping, 做映射,但由于 SimpleUrlHandlerMapping 的优先级(order)没有 DefaultAnnotationHandlerMapping 高。 所以本来应该是 foo.html 静态资源响应,被映射为了 /foo.* 。@RequestMapping 的三次映射可以通过 DefaultAnnotationHandlerMapping#setUseDefaultSuffixPattern(false) 取消。XxxxHandlerMapping 可以通过 order 属性改变。
    所以总结以上设置,可以这样正确的配置 spring-servlet-context.xml

    Xml代码 收藏代码

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:mvc="http://www.springframework.org/schema/mvc"
    5. xsi:schemaLocation=" 
    6.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    8. <context:component-scan base-package="com.myPackage"/>
    9. <mvc:resources mapping="/**" location="/" order="1"/>
    10. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    11. <property name="order" value="0"/>
    12. <property name="useDefaultSuffixPattern" value="false"/>
    13. </bean>
    14. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    15. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    16. <property name="prefix" value="/WEB-INF/pages/" />
    17. <property name="suffix" value=".jsp" />
    18. </bean>
    19. </beans>

    或者这样配置

    Xml代码 收藏代码

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:mvc="http://www.springframework.org/schema/mvc"
    5. xsi:schemaLocation=" 
    6.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    8. <context:component-scan base-package="com.myPackage"/>
    9. <mvc:resources mapping="/**/*.html" location="/" order="0"/>
    10. <mvc:resources mapping="/**" location="/" order="2"/>
    11. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    12. <property name="order" value="1"/>
    13. <property name="useDefaultSuffixPattern" value="true"/>
    14. </bean>
    15. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    16. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    17. <property name="prefix" value="/WEB-INF/pages/" />
    18. <property name="suffix" value=".jsp" />
    19. </bean>
    20. </beans>
  • 相关阅读:
    leetcode: Search in Rotated Sorted Array
    leetcode: Linked List Cycle
    leetcode: Longest Valid Parentheses
    leetcode: Next Permutation
    leetcode: Substring with Concatenation of All Words
    leetcode: Divide Two Integers
    leetcode: Implement strStr()
    Spider Studio 新版本 (20131201)
    已知问题汇总 (2013-11-30)
    C#代码获取或设置Iframe中的HTML
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/3439526.html
Copyright © 2011-2022 走看看