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.
No related posts.
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!
How do you want to get the username? Do you mean something like /mydomain/gaurav where gaurav is my username?
yep, or like mydomain/user/gaurv to say that after /user/ there will be every time the username…
thanks!
would be great for an answer. have a look at netlog e.g. the use url“s like
http://de.netlog.com//photo/photoid=
do you have an idea how to realize that?
thanks a lot!
de.netlog.com/username/photo/photoid=photoid
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!
Pardon my bad written english. But i see that my web.xml servlet mappings were not properly formated. Still waiting for your response. Thanks!
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.
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