Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {
    [...]
  }
}

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?

like image 682
Denis Bazhenov Avatar asked Mar 27 '10 08:03

Denis Bazhenov


1 Answers

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}" />
like image 55
Janning Avatar answered Oct 09 '22 02:10

Janning