7

In my web.xml I have the following mapping

<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/testMe ie any path without a file extension prefixed by articles.

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

<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

<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

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

@RequestMapping(value = "/test")

it works okay and if

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

Cheers in advance.

jaseFace
  • 1,415
  • 5
  • 22
  • 34
  • have you tried @RequestMapping(value = "/articles") ? – Cristiano Fontes Oct 10 '12 at 14:01
  • What does your DispatcherServlet configuration look like? – Peter Bratton Oct 10 '12 at 14:38
  • since you don't have the entire controller I cant' tell, but make sure you haven't also specified a RequestMapping on the controller class. – DavidA Oct 10 '12 at 14:53
  • make sure you have specified context-component scan property. – Sumit Desai Oct 10 '12 at 15:32
  • Thanks for the quick replies. I do not have an extra RequestMapping on the controller class. I also have tried @RequestMapping(value = "/articles") Please find more configuration data available in the main question. – jaseFace Oct 10 '12 at 16:59
  • mmm... It looks like all the path info is tripped by the time it hits RequestMapping if I map to just /test it works okay and if I System.out.println(request.getPathInfo()); I just get /test with no /articles ??? – jaseFace Oct 10 '12 at 18:12

1 Answers1

8

Add this to your 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 mvc namespace lines on top:

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

And the URLs in xsi:schemaLocation.

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

Check the Spring Documentation for more info.

betomontejo
  • 1,657
  • 14
  • 26
  • Thanks for the reply. I am using spring 2.5 – jaseFace Oct 10 '12 at 22:51
  • The solution should not be far from this. Look into the [`AnnotationMethodHandlerAdapter#setAlwaysUseFullPath()`](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.html#setAlwaysUseFullPath(boolean)). Sorry I'm busy at the moment, if you don't find a solution I'll test it out later in the day with Spring 2.5. – betomontejo Oct 11 '12 at 07:10