Java 和 Spring 中的 URL 重写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7976173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL Rewriting in Java and Spring
提问by Unknown
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:
我是Java和Spring的新手。我需要知道我们如何在Java和Spring中实现URL重写。例如在 .NET 环境中,我们可以使用以下代码来实现:
Global.asax.cs:
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.
同样,我们需要在Java和Spring中实现。
- Can we have anything related to this in Java and Spring?
- If we cannot do using above code,How we can achieve URL Rewriting?
- 我们可以在 Java 和 Spring 中找到与此相关的任何内容吗?
- 如果上面的代码不能用,如何实现URL Rewriting?
Help would be appreciated.
帮助将不胜感激。
回答by Ralph
I understand the question in the way, that you need an url rewrite in your server.
我理解这个问题,你需要在你的服务器中重写一个 url。
Have a look at the tuckey UrlRewriteFilter.
回答by Lincoln
I would recommend using OCPsoft Rewrite(beta) or OCPsoft PrettyFaces(final), which are newer and more evolved tools for doing Java Servlet URL-rewriting.
我建议使用OCPsoft Rewrite(测试版)或OCPsoft PrettyFaces(最终版),它们是用于进行 Java Servlet URL 重写的更新和更先进的工具。
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.
Rewrite 还支持您的 tuckey 配置,如果您想利用现有配置,并添加更强大的基于 Java 的 Rewrite 配置。
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}/"));
}
}
回答by Fla
If you're using Spring >= 3, you can use @RequestMapping. See the official documentation
如果您使用的是 Spring >= 3,则可以使用 @RequestMapping。看官方文档