7

私のweb.xmlには、次のマッピングがあります

<servlet-mapping>
    <servlet-name>mySite</servlet-name>
    <url-pattern>*.html</url-pattern> 
</servlet-mapping>

<servlet-mapping>
    <servlet-name>mySite</servlet-name>
    <url-pattern>/articles/*</url-pattern> 
</servlet-mapping>

現在、ファイル拡張子が .html の URL は正常に処理されます。ただし、タイプhttp://localhost:8080/MySite-Web/articles/testMe の URL 、つまり、記事の前にファイル拡張子が付いていない任意のパスを処理できるようにしたいと考えています。

私が試した春のマッピングは.

@RequestMapping(value = "/articles/*")
public ModelAndView getArticles(HttpServletResponse response, HttpServletRequest request
        ) throws java.lang.Exception {

    System.out.println("Handle any path prefixed with /articles/ ");
    return null;
}

私のSpring構成では、次を使用しています

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

編集:

Dispatcher Servlet のマッピングは次のとおりです。

<servlet>
        <description>
        servlet</description>
        <servlet-name>MySite</servlet-name>
        <servlet-class>
        org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:MySite-web-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

ここに MySite-web.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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/jee
      http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
      http://www.springframework.org/schema/lang
      http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">


    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    </bean>

                <context:component-scan base-package="com.mysite" scoped-proxy="interfaces" />



<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
             <value>-1</value> <!-- 10MB limit  -->
         </property>
</bean>


</beans>

/test だけにマップすると、RequestMapping に到達するまでにすべてのパス情報が削除されているように見えます

@RequestMapping(value = "/test")

それは問題なく動作し、もし

I System.out.println(request.getPathInfo()); /articlesなしで/testを取得するだけです???

事前に乾杯。

4

1 に答える 1

8

これをあなたに追加してくださいMySite-web.context.xml

<mvc:annotation-driven/>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
</bean>                

mvc適切な名前空間行を先頭に追加することを忘れないでください。

xmlns:mvc="http://www.springframework.org/schema/mvc"

および の URL xsi:schemaLocation

マッピングを解決するためのフル パスを考慮する必要があります。

詳細については、 Spring のドキュメントを確認してください。

于 2012-10-10T20:24:16.537 に答える