views:

383

answers:

2

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?

A: 

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

leeand00
I realize that the example is in PHP, but it should rewrite your url into a friendly URL anyway.(It won't matter if you're using JSP/Spring or what you plan to use.)
leeand00
(Additionally, you'll need to connect your Servlet container up to Apache, so that the requests from Apache are forwarded to your Servlet container, and the responses ported back through Apache back to the user.)
leeand00
I'm not sure if this suits his question. Either way, the Java EE way would be a Filter like this one: http://tuckey.org/urlrewrite/
BalusC
@BalusC I guess it all depends on how he wants to go about doing it, usually J2EE servers sit behind an Apache server, but not in all cases. *shrugs*
leeand00
@BalusC Post your answer in the form of an answer as well. I think both answers are good, depending on the needs of @dotsid.
leeand00
He is asking for a Spring-targeted answer. There may be Spring specific solutions. I don't do Spring.
BalusC
@BalusC "I don't do Spring" Any reason why?
leeand00
No specific reason. I just never had the need to learn it.
BalusC
+4  A: 

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}" />
Janning
Hmm... Very convenient way of url constructing. Thanks
dotsid
+1 I was having a hard time wrapping my head around @ModelAttribute-on-a-method until now. Thank you!
Ed Brannin