在我的web.xml
中,默认servlet映射即/
被映射到Spring分派器。在我的Spring调度程序配置中,我有DefaultAnnotationHandlerMapping
,ControllerClassNameHandlerMapping
和AnnotationMethodHandlerAdapter
,它允许我通过它的类名或它的@Requestmapping
注释将url映射到控制器。然而,web根目录下有一些静态资源,我也希望spring调度器使用默认的servlet来提供服务。根据Spring documentation,这可以使用<mvc:default-servlet-handler/>
标签完成。将Spring-MVC配置中的默认servlet处理程序放在哪里
在下面的配置中,我标记了4个候选位置,可以插入此标记。插入在不同的位置标记会导致调度员不同的表现如下:
案例1:如果我在位置1插入,调度员将不再能够通过@RequestMapping和控制器类名来处理映射但它通常会为静态内容提供服务。
Cas 2,3:如果其他映射无法成功完成,它将能够处理由@RequestMapping和控制器类名称进行的映射以及为静态内容提供服务。
案例4:它将无法提供静态内容。删除注意:这是一个错误,当实现一个控制器的方法映射到/**
但没有明确的控制器类名的请求映射。
因此,案例2和是可取的。据Spring文档,这个标签配置其优先顺序给予最低何必位置事项的处理程序?哪个是放这个标签的最佳位置?
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="webapp.controller"/>
<!-- Location 1 -->
<!-- Enable annotation-based controllers using @Controller annotations -->
<bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- Location 2 -->
<bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- Location 3 -->
<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- Location 4 -->
<mvc:default-servlet-handler/>
<!-- All views are JSPs loaded from /WEB-INF/jsp -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
但你无法通过这个标签设置此映射的顺序。 – gigadot 2011-01-07 11:01:29