弹簧

2012-10-10 119 views
5

在我的web.xml servlet映射我有以下映射弹簧

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

目前,它处理URL与精细的.html文件扩展名。但是,我希望能够处理类型为的网址http:// localhost:8080/MySite-Web/articles/testMe即没有文章前缀为文章的任何路径。

我试过的春季贴图是。

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

编辑:

这里的调度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> 

它看起来像所有的路径信息被它击中RequestMapping的时间,如果我映射到刚刚/测试

@RequestMapping(value = "/test") 

它工作很好,而且剥夺如果

我的System.out.println( request.getPathInfo());我刚刚拿到/test没有/文章 ???

提前欢呼。

+0

你有没有试过@RequestMapping(value =“/ articles”)? –

+0

您的DispatcherServlet配置是什么样的? –

+0

因为你没有整个控制器我不能'告诉,但确保你还没有在控制器类上指定一个RequestMapping。 – DavidA

回答

7

添加到您的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的URL。

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

查看Spring Documentation了解更多信息。

+0

感谢您的回复。我使用弹簧2.5 – jaseFace

+0

解决方案不应该离这很远。查看['AnnotationMethodHandlerAdapter#setAlwaysUseFullPath()'](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.html#setAlwaysUseFullPath (布尔值))。对不起,我现在很忙,如果你没有找到解决方案,我将在今天晚些时候用Spring 2.5进行测试。 – betomontejo

+0

现货。谢谢 – jaseFace