java 使用 spring 的 Servlet 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12821079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:21:05  来源:igfitidea点击:

Servlet mapping with spring

javaspringservlets

提问by jaseFace

In my web.xml I have the following mapping

在我的 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>

Currently it handles urls with a file extension of .html fine. However, I want to be able to handle urls of the type http://localhost:8080/MySite-Web/articles/testMeie any path without a file extension prefixed by articles.

目前,它可以很好地处理文件扩展名为 .html 的 url。但是,我希望能够处理类型为http://localhost:8080/MySite-Web/articles/testMe 的 url,即任何没有以文章为前缀的文件扩展名的路径。

The spring mapping I have tried is.

我尝试过的弹簧映射是。

@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;
}

in my Spring configuration I am using the following

在我的 Spring 配置中,我使用了以下内容

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

edit:

编辑:

Here's the Dispatcher Servlet mapping

这是 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>

here's the MySite-web.context.xml

这是 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>

It looks like all the path info is stripped by the time it hits RequestMapping if I map to just /test

如果我只映射到 /test,那么在它点击 RequestMapping 时,所有路径信息似乎都被删除了

@RequestMapping(value = "/test")

it works okay and if

它工作正常,如果

I System.out.println(request.getPathInfo()); I just get /testwith no /articles???

I System.out.println(request.getPathInfo()); 我只是在没有/articles 的情况下获得/test???

Cheers in advance.

提前加油。

回答by betomontejo

Add this to your MySite-web.context.xml:

将此添加到您的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>                

Don't forget to add the proper mvcnamespace lines on top:

不要忘记mvc在顶部添加正确的命名空间行:

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

And the URLs in xsi:schemaLocation.

以及xsi:schemaLocation.

It should now take into consideration the full path for resolving mappings.

它现在应该考虑解析映射的完整路径。

Check the Spring Documentationfor more info.

查看Spring 文档以获取更多信息。