5

I have created a mapping in web.xml something like this:

<servlet>  
        <servlet-name>dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
</servlet>
<servlet-mapping>  
        <servlet-name>dispatcher</servlet-name>  
        <url-pattern>/about/*</url-pattern>  
</servlet-mapping>

In my controller I have something like this:

import org.springframework.stereotype.Controller;  
@Controller  
public class MyController{  
    @RequestMapping(value="/about/us", method=RequestMethod.GET)
    public ModelAndView myMethod1(ModelMap model){  
        //some code  
        return new ModelAndView("aboutus1.jsp",model);  
    }  
    @RequestMapping(value="/about", method=RequestMethod.GET)
    public ModelAndView myMethod2(ModelMap model){  
        //some code  
        return new ModelAndView("aboutus2.jsp",model);  
    }  
}

And my dispatcher-servlet.xml has view resolver like:

<mvc:annotation-driven/>  
<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"/>

To my surprise: request .../about/us is not reaching to myMethod1 in the controller. The browser shows 404 error. I put a logger inside the method but it isn't printing anything, meaning, its not being executed.
.../about works fine! What can be the done to make .../about/us request work? Any suggestions?

Atharva
  • 6,711
  • 5
  • 31
  • 39

3 Answers3

12

You need to use @RequestMapping(value="/us", method=RequestMethod.GET) or you need to request about/about/us

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Ravi Khakhkhar
  • 1,924
  • 1
  • 18
  • 25
  • But I haven't mapped any thing on the controller class level. Do I need to ? I think only then I need to writer `/us` instead of `/about/us` and have a look: `/about` is already working ! Then why not `about/us`. – Atharva Jun 26 '12 at 09:33
  • 3
    The reason : You have mapped **/about/** in web.xml to DispatcherServlet. So, you need to map only part following **/about**. – Ravi Khakhkhar Jun 26 '12 at 09:39
  • Do **/about** coming into your method ?? Really?? It must be coming into DispatcherServlet – Ravi Khakhkhar Jun 26 '12 at 09:51
  • It's leading to the method through the dispatcher servlet. By the way I got the code working by adding few more config lines in the dispatcher-servlet.xml. Take a look at my answer. Thanks for your help. :) – Atharva Jun 26 '12 at 10:52
  • Congratulations.!!! But, I have different view on this. I don't see any reason why I need to use **alwaysUseFullPath**, as long as I can map only later part of the request URLs for Spring. At least I don't need to repeat **/about** everywhere. – Ravi Khakhkhar Jun 26 '12 at 11:05
  • Well I added them after trying out all cases possible in mapping. I think I need to understand when do we use alwaysUseFullPath. I just added it by seeing similar thing in some other post. – Atharva Jun 26 '12 at 11:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13044/discussion-between-ravi-khakhkhar-and-atharva) – Ravi Khakhkhar Jun 26 '12 at 11:13
  • Okay I understood it. I would prefer to use alwaysUseFullPath option as it makes the things explicit in the RequestMappings in the controller methods. This is especially when dispatcher servlet mappings are very much generic and bound to many url-patterns. BTW, you don't need to write `about` everywhere in the methods. Just annotating it once over the class is enough. And in the inside methods we can just use relative url patterns like `/us`. – Atharva Jun 26 '12 at 11:38
  • Later on, you will realize that this is not the standard.And at least I have to repeat it on every class :P – Ravi Khakhkhar Jun 26 '12 at 15:21
2

Since you have mapped "/about" in your web.xml, the url it will pass will be like this www.xyz.com/about/*

As your configuration says it will work for

  1. www.xyz.com/about/about/us
  2. www.xyz.com/about/about

In order to to work properly either use /* in web.xml instead of /about

or change the controller's endpoint to

@RequestMapping(value="/us", method=RequestMethod.GET)

@RequestMapping(value="/", method=RequestMethod.GET)

Siddharth
  • 9,349
  • 16
  • 86
  • 148
ravi ranjan
  • 5,920
  • 2
  • 19
  • 18
-3

Okay I got the thing working, here are things I added in the dispatcher-servlet.xml:

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="alwaysUseFullPath" value="true" />
    </bean>

    <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="alwaysUseFullPath" value="true" />
</bean>
Atharva
  • 6,711
  • 5
  • 31
  • 39