Thursday, July 8, 2010

How to URL Rewrite in Spring MVC

I am in the process of writing a Groovy based web app with Spring MVC and I wanted to use URL Rewriting. I scoured the boards for HOWTO's and from all my research Tuckey.org's URLRewrite is the best one out there. Here's a quick tutorial on how to set it up.

First, you need to add the dependency to your pom:


Next you need to map the URL Rewrite filter and your Spring Dispatcher servlet in your web.xml:

Then in src/main/webapp/WEB-INF you need to define a urlrewrite.xml. This file defines the filters for your webapp. For my app, I keep all JSP files under WEB-INF/jsp and use Spring's InternalResourceViewResolver to forward to them. This is great, except since my filter takes all request, I have to supply rules for the static files like html, javascript, css, images etc.

Here's how you do that. In urlrewrite.xml add the following:

That's it, URL rewriting is enabled on your app. Now anytime someone hits a URL in your app like http://[server]:[port]/[context-root]/[controller]/[action] (depending on which HandlerMapping you use) the filter will forward the request to the dispatcher servlet and your appropriate controller. For anything that goes to /images /js /css or /html, they will be forwarded to those resources.

I hope this is helpful.

14 comments:

  1. Worked like a charm! Thanks!

    ReplyDelete
  2. wow, thanks it's working, awesome...

    ReplyDelete
  3. Hi Matt,
    I am working on a project that involves Freemarker and Spring MVC and in the presentation layer all the pages have a ".ftl" extention. Now a late requirement came up where they want me to remove all those extensions and make all the urls extentionless. So for example if a url used to be : "http://localhost:8080/index.ftl", they want the new url to be "http://localhost:8080/index".
    UrlRewriteFilter is new to me. Any help on how I can configure the rule will be very much appreciated.
    Thanks,

    ReplyDelete
  4. @Emmanuel - Check out this code => https://bitbucket.org/mgivney/blog-tuckey-question

    I answered your question in the README and put together a sample to demonstrate it.

    HTH,
    Matt

    ReplyDelete
  5. Thanks Matt, I really appreciate it. The rule worked however the app cannot find the css, images and js that are under the WebContent folder but I will figure it out.
    Again thanks for your time Matt.

    Emmanuel

    ReplyDelete
  6. @Emmanuel No problem. For CSS, IMAGES and JS, take a look at the ruleset I posted in this blog post. It should be as simple as a cut and paste. Good luck.

    ReplyDelete
  7. Matt,
    You rock. LOL.
    It did work. Thanks a ton for your help on this.

    ReplyDelete
  8. Hi,
    My images/styles/js are under a folder called resources parallel to Web-Inf.

    my url is something like this localhost:8080/project/restaurantname/home

    which i have been able to change to
    localhost:8080/project/home.


    /restaurantname/**
    /$1


    But rules for images and url, outdo each other, they work seperately but not together.
    Please help me.


    /images/**
    /images/$1

    ReplyDelete
  9. @Amita - I am not sure I am following your question, but could you not change your "to" node to /resources/images/$1, /resources/js/$1 etc?

    ReplyDelete
  10. Hi Matt, Is it possible to get the image from a different server path other than web-app path, the client is making a http call to display the image.

    Pleaese help.

    ReplyDelete
    Replies
    1. Is the image on a web server? If so, you could provide the full path to the image in your html: . Since it isn't part of the web app, you don't need to define it in the urlrewrite.xml. It won't hit your filter.

      Delete
  11. Thanks Matt, works for me...
    /web/$1

    what is the $1 here? a wildcard...

    What does it signify...

    ReplyDelete
    Replies
    1. Yes, it is equivalent to saying /web/*

      Delete
  12. This comment has been removed by the author.

    ReplyDelete