私のweb.xml
には、デフォルトのサーブレットマッピング(/
)がSpringディスパッチャにマッピングされています。 Springのディスパッチャ設定では、私はDefaultAnnotationHandlerMapping
、ControllerClassNameHandlerMapping
、AnnotationMethodHandlerAdapter
を持っていますので、クラス名または@Requestmapping
注釈でコントローラにURLをマップできます。しかし、Webルートの下に静的なリソースがあり、デフォルトのサーブレットを使ってSpringのディスパッチャを提供したいと思っています。 Spring documentationによれば、これは<mvc:default-servlet-handler/>
タグを使用して行うことができます。Spring MVCコンフィグレーションでdefault-servlet-handlerを配置する場所
以下の設定では、このタグを挿入することが可能な4つの候補地点があります。
ケース1:別の場所にタグを挿入すると、ディスパッチャは、次のように異なる動作をさせ、私は場所1でそれを挿入した場合、ディスパッチャはもはや@RequestMappingとコントローラのクラス名でマッピングを処理することができません静的なコンテンツを正常に配信します。
Cas 2、3:@RequestMappingとコントローラクラス名によるマッピングを処理することができます。また、他のマッピングが正常に実行できない場合は静的コンテンツを提供することができます。
ケース4:静的コンテンツを配信することはできません。削除の注意:/**
にマッピングされたメソッドを持つコントローラを実装すると、コントローラクラス名に明示的な要求マッピングがないというバグです。
したがって、ケース2とは、ドキュメントを春にすることが望ましい.Accordingあり、このタグは、優先順位が最も低いので、なぜ位置事項に与えられているハンドラを設定しますか?このタグを置くのに最適な位置はどれですか?
<?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
@gigadot:この順序は、Bean定義の順番で暗黙的です。 – skaffman
場所3と4の間に違いはないということは間違いありません。私は質問と回答を更新しました。 Springは、 'mvc:default-servlet-handler'タグで設定されたハンドラマッピングに最低の優先順位を与えますが、明示的な値が設定されていない場合、他のハンドラマッピングにも同じ値を与えます。ご連絡ありがとうございます。 – gigadot