2

I am new to Java and spring.I need to know how we can achieve URL rewriting in Java and Spring. For example in .NET environment we can achieve this by using following code:

Global.asax.cs:

protected void Application_BeginRequest(object sender, EventArgs e) {
  try {
    string fullOrigionalpath = Request.Url.ToString();
    if (fullOrigionalpath.Contains("/Home-Page")) {
      Context.RewritePath("~/home.aspx"); return;
    }
  }
}

Similarly,we need to achieve in Java and Spring.

  1. Can we have anything related to this in Java and Spring?
  2. If we cannot do using above code,How we can achieve URL Rewriting?

Help would be appreciated.

JanS
  • 2,065
  • 3
  • 27
  • 29
Kiran
  • 20,167
  • 11
  • 67
  • 99
  • possible duplicate of [Spring - Rewrite one URL to another](http://stackoverflow.com/questions/2330630/spring-rewrite-one-url-to-another) –  Nov 02 '11 at 05:44
  • @RC.. Actually i need to know that whether is there any thing like above code in JAVA and spring or else how we can achieve.So AFAIK this is not a duplication of what you mentioned. – Kiran Nov 02 '11 at 05:53
  • 1
    I'd say the accepted answer is exactly what you want. – Mark Rotteveel Nov 02 '11 at 08:52
  • ok but i have one more doubt called can we achieve URL rewriting by using some funcitons like above in java and spring? – Kiran Nov 02 '11 at 09:02

2 Answers2

2

If you're using Spring >= 3, you can use @RequestMapping. See the official documentation

Fla
  • 536
  • 6
  • 23
2

I would recommend using OCPsoft Rewrite (beta) or OCPsoft PrettyFaces (final), which are newer and more evolved tools for doing Java Servlet URL-rewriting.

Rewrite also has support for your tuckey configuration, if you want to take advantage of your existing configuration, and add in more powerful Java-based Rewrite configuration.

It is very stable and well tested.

package com.example;
public class ExampleConfigurationProvider extends HttpConfigurationProvider
{
   @Override
   public int priority()
   {
     return 10;
   }

   @Override
   public Configuration getConfiguration(final ServletContext context)
   {
     return ConfigurationBuilder.begin()
       .defineRule()
         .when(Direction.isInbound().and(Path.matches("/some/{page}/.*/")))
         .perform(Redirect.permanent("/new-{page}/"));
    }
}
Lincoln
  • 3,151
  • 17
  • 22