开发者

Is there a Java equivalent for Ruby on Rails "url_for"?

开发者 https://www.devze.com 2022-12-21 02:30 出处:网络
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 cla

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:

  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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号