Spring+JSP url building best practices
I wonder if there are any good practices for addressing Spring controllers in JSP.
Suppose I have controller:
@Controller
class FooController {
// Don't bother about semantic of this query right now
@RequestMapping("/search/{applicationId}")
public String handleSearch(@PathVariable String applicationId)开发者_StackOverflow社区 {
[...]
}
}
Of course in JSP I can write:
<c:url value="/search/${application.id}" />
But it's very hard to change url then. If you familiar with Rails/Grails then you now how this problem resolved:
redirect_to(:controller => 'foo', :action = 'search')
But in Spring there is so much UrlMappers. Each UrlMapper have own semantic and binding scheme. Rails alike scheme simply doesn't work (unless you implement it yourself). And my question is: are there any more convenient ways to address controller from JSP in Spring?
I hope i understood your question. I think your asking about how to maintain urls when url strings are in jsp and controller mappings.
Your Controller should do the logic, your JSP should do the output. Constructing an Url should be the responsability of the controller handling it. So
class SearchController {
@RequestMapping("/search/{applicationId}")
public String handleSearch(@PathVariable String applicationId) {
[...]
}
public String getUrl(Long applicationId) {
return "/search/" + applicationId;
}
}
class StartController {
private SearchController controller;
@ModelAttribute("searchUrl")
public String getSearchUrl() {
return fooController.getUrl(applicationId);
}
}
and in your start.jsp do
<c:url value="${searchUrl}" />
Try using Apache as a front end to remap the URLs:
http://www.simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/
This way you can change the applicationId parameter from a query string into a friendly URL.
Additionally, here is the documentation for mod_rewrite: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
精彩评论