views:

299

answers:

3

For example, with the URL

foo.com/bar/99

99 will be available directly to a method in the controller as an argument. The controller is mapped to /bar

For anyone familiar with ASP.NET MVC or Django, this would be similar to routes.MapRoute in the former and using (?P\d+) in urlpatterns in the latter.

It would be possible to process the data in the Http Request object directly to get this, but I'd like to know if Spring MVC has built-in support for this (particularly version 2.5).

A: 

No, Spring does not support this out of the box. You can, however, map path to method name using @RequestMapping and appropriately configured InternalPathMethodNameResolver:

@Controller
@RequestMapping("/bar/*")
public class MyController {
  ...
  public String do99(HttpServletRequest request) {
    ...
  }
}

You'll have to specify prefix for InternalPathMethodNameResolver as "do" for the above to work since method names can't begin with digit. If you were to use "foo.com/bar/baz" as URL and call your method baz, no additional configuration would be necessary.

If you don't want to use method name mapping, getting '99' as parameter is as easy as grabbing it from request.getPathInfo() in your method.

ChssPly76
Thank you. This is what I was starting to believe but I hoped I had just missed something obvious. The "99" in my example would be a variable, e.g. an item id, so the InternalPathMethodNameResolver solution will not work. I will probably try creating a custom resolver to parse the pathInfo.
Donal Boyle
The problem with custom resolver is that method parameter type are pretty much predefined in Spring: http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#mvc-ann-requestmapping-arguments There are many choices but none of them would do what you want. It would take some heavy lifting in `AnnotationMethodHandlerAdapter` to change that and you're likely looking at defining your own annotation type too. All that assumes you want to keep your controllers independent, though - if you're ok with having a base class to provide this functionality it should be trivial to do this.
ChssPly76
After playing with it for a bit I agree. Useing getPathInfo and processing the string in the controller is much more straight-forward than writing a handler.
Donal Boyle
+5  A: 

For anyone using Spring 3, I've learned this can be done using the new @PathVairable annotation

Here's the example from http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

@RequestMapping("/hotels/{hotelId}")
public String getHotel(@PathVariable String hotelId, Model model) {
    List<Hotel> hotels = hotelService.getHotels();
    model.addAttribute("hotels", hotels);
    return "hotels";
}
Donal Boyle
A: 

Hi what if you just wanted a URL like: abcc.com/hello abcc.com/mikey

how could I have a generic controller (apart from all the other ones that actually map to something i.e. abcc.com/register)??

so in other words, I believe the generic could always be called... if "hello" exists in the app, then it would forward to the hello page but if it didnt, it would give a 404 error or something. How can this be done.... same idea as twitter ie..twitter.com/justin

thanks