URL Rewriting in Java and Spring

核能气质少年 提交于 2020-12-12 11:41:07

问题


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.


回答1:


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}/"));
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/7976173/url-rewriting-in-java-and-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!