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) {
[...]
}
}
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}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With