Mapping many/all URL’s to a single spring controller using annotations

Once in a while this kind of a question pops up on the spring forums. The requirement is almost always the same : Map many URLs to a single controller and/or map all URL’s without a defacto mapping to a default controller. Both have different solutions.

Let’s discuss the first one. The @RequestMapping annotation makes configuring spring controllers a breeze. The value param of the annotation accepts a string array of url’s, most of us miss this part. To map multiple URL’s to a single controller all you need to do is :

@Controller
@RequestMapping(value={"/changepassword.html","/admin/changepass.html"})
public class ChangePasswordForm {

// methods
}

You can also use ant-style regular expressions to map URL’s.

@Controller
@RequestMapping(value={"/changepassword.html","/admin/changepass.html", "/change*.html"})
public class ChangePasswordForm {

// methods
}

Now onto mapping ALL URL’s which do not have a default mapping to a particular controller. This is also quite simple, all you need to do is create a controller which maps to everything. The way spring-mvc works is that it looks for an absolute mapping to service every request, if it doesn’t find one, it then asks the best match to service the request. The controlller should look something like this :

@Controller
@RequestMapping("/*")
public class ChangePasswordForm {

// methods
}

Do leave a comment if you run into a scenario I haven’t covered here or if the post has been of help to you. :)

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • Reddit
  • Digg
  • Facebook
  • Netvibes
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Print
  • Slashdot
  • Share/Bookmark

No related posts.

9 Responses to “Mapping many/all URL’s to a single spring controller using annotations”

  1. daniweil says:

    hi. first of all great blog and nice style.
    just one more scenario:
    say we want to retrieve user with a specific mapping like
    mydomain/u/
    so we can map that like above described, but what about the username? how
    to get it? is there a possibility without splitting the http request?
    thanks for your comment!

  2. Adedayo says:

    Hi, i have got a littl e bit of a challenge. I av a controller with serverl mappings within it. Using method level request mappings annotations for an annotated Class. Now wen i a method with @RequestMapping(value={”/fbeasysms/*”,”/fbwelcome.do”}),thing is now i get all request with context /fbeasysms being mapped to this method a valid method handler with @RequestMapping(”/fbeasysms/fbmainpage”) is still being directed to the first method handler mentioned earlire. Any tip would be of help. Lastly meth web.xml looks like this:
    easybuy
    *.do

    easybuy
    /fbeasysms/*

    easybuy
    /fbeasysms

    when i tried add :

    easybuy
    /fbeasysms/*.do

    or
    easybuy
    /fbeasysms/*.fb

    I get error messages at startup. Tahnks!

  3. Adedayo says:

    Pardon my bad written english. But i see that my web.xml servlet mappings were not properly formated. Still waiting for your response. Thanks!

  4. Vikram says:

    I find your articles and tutorial of extremely great help. Precise on knowing exactly what I want to know. I just had a problem though, I created @RequestMapping( “/user/* ) on my class level and then at the method level I defined individual RequestMapping with different urls such as @RequestMapping( “saveUser.htm” ). Spring docs say we can do this to filter out in the class which method should be invoked upon which URL.

    Somehow the mapping jst isn’t working for the class level. :(

  5. PUK says:

    The RequestMapping of /* at the controller-level doesn’t work. I’m using Spring 3.0.0.RC3. The container’s throwing a HTTP 500 with the following:

    javax.servlet.ServletException: No adapter for handler [org.actest.web.DefautController@1912965]: Does your handler implement a supported interface like Controller?

    If I put the /* at the method level, it works.

    Also, can you tell me where you read that Spring looks for more specific mapping before the more general case? I’m interested in having a default catch-all handler.

    Thanks,

    PUK

Leave a Reply