Results 1 to 5 of 5

Thread: @RequestMapping not working as I expect

  1. #1

    Unhappy @RequestMapping not working as I expect

    Hi,

    Using Spring 2.5.6, Java 1.6 in Jetty 6.21.

    applicationContext-mvc.xml (used by web.xml, DispatcherServlet)
    Code:
        <context:component-scan base-package="foo.bar.web"/>   
    
        <!-- INITIALISE THE ANNOTATED CONTROLLER HANDLERS -->
    
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    My Controller class:
    Code:
    @Controller
    @RequestMapping("/isp")
    public class ISPController {
    
      @Autowired
      private ISPService ispService;
    
      @RequestMapping(method = RequestMethod.GET)
      public void index(final Model model) {
         final List<ISP> isps = ispService.findAll();
         model.addAttribute("ISPs", isps);
         // I have a tiles definition of "isp/index"
      }
    
      @RequestMapping(value="/view", method = RequestMethod.GET)
      public void view(@RequestParam("id") final Long id, final Model model) {
        final ISP isp = ispService.find(id);
        model.addAttribute("ISP", isp);
       // I have a tiles definition of "isp/view"
      }
    This is the problem:

    If I try:

    http://localhost:8080/isp --> underlying page renders (tiles definition : isp/index)

    http://localhost:8080/isp/ --> 404, No Mapping found for HTTP request with URI (/isp/) in DispatcherServlet with name 'dispatcher'

    http://localhost:8080/isp/view?id=1 --> 404, No Mapping found for HTTP request with URI (/isp/view) in DispatcherServlet with name 'dispatcher'

    I really don't understand why this doesn't work. I thought that all @RequestMappings at method level were relative to the @RequestMapping at the type level. I don't understand why /isp works but /isp/view or /isp/ doesn't.

    I have similar methods, i.e., create, delete etc., and if I try to go to http://localhost:8080/isp/create or http://localhost:8080/isp/delete I get the same 404 errors.

    I would really appreciate any help that can shed some light on this for me.

    Yours most gratefully,

    -=bootlaces=-

    BTW: I tried removing value="view" from the @RequestMapping for the view method and it didn't work.
    Last edited by bootlaces; Dec 3rd, 2009 at 09:15 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    /isp and /isp/ are, how strange it might sound, different mappings. If you want more specific mappings you need to include a * on the root level (like /isp/*). It is explained in the mvc chapter, I agree not really extensive...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Smile

    Quote Originally Posted by Marten Deinum View Post
    /isp and /isp/ are, how strange it might sound, different mappings. If you want more specific mappings you need to include a * on the root level (like /isp/*). It is explained in the mvc chapter, I agree not really extensive...
    Hi Marten,

    Thanks, as always for a prompt reply. I've just stepped thru the code using Eclipse (and the wonderful power of Ivy to download the sources automatically) to try and understand what is going on.

    Indeed, within AbstractUrlHandlerMapping.lookupHandler(urlPath, request), it's trying to do a bestPathMatch using AntPathMatcher.

    Coming in is the urlPath : /isp/view
    Registered Handler in the map is : /isp.*

    This, of course, fails the bestPathMatch.

    If, in my controller, I now put:

    @Controller
    @RequestMapping("/isp/**")

    Then, my path /isp/view is matched against /isp/** and it finds the appropriate handler in the handler map. Ultimately, the method called "view" in the controller gets fired. Result!

    Is this the default behaviour for Spring 3 as well? I ask this because in the Reference Manual Reference Manual it is not clear, i.e., it seems to say that I don't need to put in /appointments/** for the @RequestMapping on the Controller, so therefore, it *should* match to any path "as-is" (i.e., with /appointments as it is, then using /appointments/new it should just work)

    Just a request on clarification for this. One last thing.

    How *do* I map properly?

    Basically I want:

    /isp -> isp/index
    /isp/ -> isp/index
    /isp/view -> isp/view

    I think I've handled the /isp/ and /isp/view now using the "/isp/**" on the @RequestMapping, but how to handle /isp only??

    Thank you for your continued and appreciated assistance :-)

    -=bootlaces=-

  4. #4
    Join Date
    Oct 2009
    Posts
    9

    Thumbs down

    In Spring 3.0, the annotation bindings have greatly improved in this area!

    I upgraded from 2.5 to 3.0 specifically for this reason.

    1) You can use @RequestMapping("/foo") simply using annotations, and with a UrlPattern of "/*" which is not possible in 2.5.

    In 2.5 you can do this, but you must have a prefix.. So instead of using "/*" as the patter you would use "/bar/*" as the pattern and this would act as the only way to go extensionless in the URLs (not using .do or .action or something at the end).

    2) You can use URL Templates and PathVariables.
    Path variables are very very helpful.

    @RequestMapping("/foo/bar/{userID}")
    Then in the action's input parameters you would use "@PathVariable String userID" and it grabs it for you.. Or what ever type your variable is... I prefer getting them as Strings and working with them inside the action method...

  5. #5

    Default

    Thanks, HolmesSPH. I'm waiting until Spring 3 is GA before I upgrade to it...but yes, I need that functionality, but I can wait a few days...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •