【问题标题】:Servlet mapping with springServlet 映射与 spring
【发布时间】:2012-10-10 13:56:25
【问题描述】:

在我的 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,即任何没有以文章为前缀的文件扩展名的路径。

我试过的spring贴图是。

@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());我只是得到 /test 而没有 /articles ???

提前干杯。

【问题讨论】:

  • 你试过@RequestMapping(value = "/articles") 吗?
  • 您的 DispatcherServlet 配置是什么样的?
  • 因为您没有完整的控制器,我无法告诉您,但请确保您还没有在控制器类上指定 RequestMapping。
  • 确保您已指定上下文组件扫描属性。
  • 感谢您的快速回复。我在控制器类上没有额外的 RequestMapping。我也试过@RequestMapping(value = "/articles") 请在主要问题中找到更多可用的配置数据。

标签: java spring servlets


【解决方案1】:

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

还有xsi:schemaLocation中的网址。

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

查看Spring Documentation 了解更多信息。

【讨论】:

  • 感谢您的回复。我正在使用弹簧 2.5
  • 解决方案应该离这个不远了。查看AnnotationMethodHandlerAdapter#setAlwaysUseFullPath()。抱歉,我现在很忙,如果您没有找到解决方案,我将在当天晚些时候使用 Spring 2.5 进行测试。
猜你喜欢
  • 2016-01-02
  • 1970-01-01
  • 2016-12-31
  • 2013-10-13
  • 1970-01-01
  • 2012-12-23
  • 2011-01-18
  • 2016-06-30
  • 2015-03-01
相关资源
最近更新 更多