views:

88

answers:

2

I have written something like this pretty easily in C# (string GetUrl(new { controller = "foo", action = "bar", baz = "fnord" }), based on the existing capabilities of the XmlRouteCollection class provided by the ASP.NET MVC framework (why it isn't there out of the box is beyond me; the additional required code was trivial). I am now faced with a JSP project, and I need the same ability: centralize the logic for generating all URLs in one place, based on a list of routing rules. Is there some code somewhere I could reuse/adapt to do this in Java? It seems like a common enough requirement, but google proved surprisingly unhelpful in finding something like this.

A: 

Raw JSP doesn't provide such functionality. There a two choice:

  1. use an exists framework that supports this (like a grails or play),
  2. or implement it by yourself, but it's no so easy, just because you need to implement also all other parts of RoR for this.
splix
I'm forced to use a particular framework so using something like grails or play won't work :-( I guess I'll have to implement it myself...
Oren Ben-Kiki
oh, it's really hard to implement, i'd tried some times ago :(
splix
Hard doesn't mean impossible right? =D
jpartogi
A: 

JSP is just a Java based view technology, it is not a MVC framework, you can best compare JSP with "Classic ASP". The Java EE counterpart of ASP.NET-MVC is JSF (JavaServer Faces). I know JSF thoroughly, but I don't seem to recognize the part what you need. It seems more to be RESTFul-flavored. In that case, have a look to Spring 3.0 MVC. It provides "URI-template" annotations to listen on certain RESTFul requests. True, this is also not really what you're looking for, but it might give you some new insights and ideas.

BalusC
Spring URI templates are almost exactly what I need, except that they work in the wrong direction. They parse a URL and match it with an action; I need to specify an action and generate a URL. At any rate, this is academic; I can't use Spring, I'm stuck with a commercial framework (that has many other things, but not this).
Oren Ben-Kiki