Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet mapping with spring

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.

like image 502
jaseFace Avatar asked Oct 10 '12 13:10

jaseFace


1 Answers

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.

like image 106
betomontejo Avatar answered Oct 02 '22 07:10

betomontejo