Spring MVC 配置请求的默认处理器

来源:互联网 发布:war包和源码区别 编辑:程序博客网 时间:2024/05/22 17:24
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 version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <!-- Processes application requests -->    <servlet>        <servlet-name>appServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-servlet-context.xml</param-value>        </init-param>        <load-on-startup>0</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>appServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

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

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

package com.myPackage.controller;@Controllerpublic class FooController{/*** 响应 /foo 请求,并返回 bar.jsp 做为渲染页面 (不一定非是jsp,只不过以上配置的是jsp)*/@RequestMappting("/foo")public String foo(){    //process logic    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,图片等),需要做如下配置

<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 version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:mvc="http://www.springframework.org/schema/mvc"              xsi:schemaLocation="        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><context:component-scan base-package="com.myPackage"/>    <mvc:resources mapping="/**" location="/" order="1"/><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">        <property name="order" value="0"/>        <property name="useDefaultSuffixPattern" value="false"/>    </bean><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">       <property name="prefix" value="/WEB-INF/pages/" />                      <property name="suffix" value=".jsp" />              </bean></beans>

或者这样配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:mvc="http://www.springframework.org/schema/mvc"              xsi:schemaLocation="        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><context:component-scan base-package="com.myPackage"/>    <mvc:resources mapping="/**/*.html" location="/" order="0"/><mvc:resources mapping="/**" location="/" order="2"/><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">        <property name="order" value="1"/>        <property name="useDefaultSuffixPattern" value="true"/>    </bean><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">       <property name="prefix" value="/WEB-INF/pages/" />                      <property name="suffix" value=".jsp" />              </bean></beans>

转自:http://bastengao.iteye.com/blog/1165486


原创粉丝点击