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 ne开发者_C百科ed 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.
Raw JSP doesn't provide such functionality. There a two choice:
- use an exists framework that supports this (like a grails or play),
- 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.
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.
精彩评论