java Spring - 将一个 URL 重写为另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2330630/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:38:46  来源:igfitidea点击:

Spring - Rewrite one URL to another

javaspringspring-mvcurl-rewritingprettyfaces

提问by George

I have a Spring 2.5 application that contains a Flash banner. I don't have the source for the Flash component but it has links hardcoded to certain pages that end in .htmlI want to be able to redirect those .html pages to existing jsp pages. How can I have Spring resolve a few .html pages to .jsp pages?

我有一个包含 Flash 横幅的 Spring 2.5 应用程序。我没有 Flash 组件的源代码,但它具有硬编码到某些页面的链接,这些页面以.html我希望能够将这些 .html 页面重定向到现有的 jsp 页面。我如何让 Spring 将一些 .html 页面解析为 .jsp 页面?

My project looks like:

我的项目看起来像:

WebContent
 |
 -sample.jsp
 -another.jsp
  WEB-INF
  |
  -myapp-servlet.xml
  -web.xml

I want localhost:8080/offers.htmlto redirect to localhost:8080/sample.jsp

我想localhost:8080/offers.html重定向到localhost:8080/sample.jsp

Can I do this with Spring? I already have a SimpleUrlHandlerMapping and UrlFilenameViewController defined in the myapp-servlet.xml that has to continue serving the pages it already is.

我可以用 Spring 做到这一点吗?我已经在 myapp-servlet.xml 中定义了一个 SimpleUrlHandlerMapping 和 UrlFilenameViewController,它们必须继续为已经存在的页面提供服务。

In my web.xml, I have

在我的 web.xml 中,我有

<servlet-mapping>
  <servlet-name>myapp</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

Update

更新

Here is the URL mapper. If I add a controller, how do I return the jsp view that is in the WebContent directory as the view resolver includes the /WEB-INF/jsp directory.

这是 URL 映射器。如果添加控制器,如何返回 WebContent 目录中的 jsp 视图,因为视图解析器包含 /WEB-INF/jsp 目录。

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/page1.htm">page1Controller</prop>
      <prop key="/page2.htm">page2Controller</prop>
    </props>
  </property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

回答by Hans Westerbeek

I think you could benefit from the open source URL Rewriting library made by tuckey.org. The guys at SpringSource endorse this library, since it is set up for you automatically if you use Spring Roo to create a project, so it is of good quality. I have used it successfully in a number of projects.

我认为您可以从 tuckey.org 制作的开源 URL 重写库中受益。SpringSource 的人认可这个库,因为如果您使用 Spring Roo 创建项目,它会自动为您设置,因此它的质量很好。我已经在许多项目中成功地使用了它。

See herefor its homepage. And Skaffman is right, you want it to 'forward' instead of redirect, which is the default behaviour.

这里为它的主页。Skaffman 是对的,您希望它“转发”而不是重定向,这是默认行为。

Configure it in web.xml like this:

在 web.xml 中像这样配置它:

<filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>

Then, in WEB-INF/urlrewrite.xml have an element like this:

然后,在 WEB-INF/urlrewrite.xml 中有一个这样的元素:

<rule>
    <from>offers.html</from>
    <to>offers.jsp</to>     
</rule>

回答by Lincoln

I would use OCPsoft PrettyFaces or OCPsoft Rewrite for this:

我会为此使用 OCPsoft PrettyFaces 或 OCPsoft Rewrite:

With PrettyFaces:

使用漂亮的面孔

create WEB-INF/pretty-config.xml

创建 WEB-INF/pretty-config.xml

<url-mapping>
   <pattern value="/offers.html" />
   <view-id value="/offers.jsp" />
</url-mapping>

With Rewrite:

使用重写

ConfigurationBuilder.begin()
   .addRule(Join.path("/offers.html").to("/offers.jsp"));

I hope this helps.

我希望这有帮助。

~Lincoln

~林肯

回答by skaffman

Firstly, I'm assuming that when you say "redirect", you really mean "forward". HTTP Redirects would not be appropriate here.

首先,我假设当你说“重定向”时,你的意思是“转发”。HTTP 重定向在这里不合适。

SO given that, here are some things to try:

鉴于此,这里有一些可以尝试的事情:

  • Can't you just move the JSP files from WebContentinto /WEB-INF/jsp/? You wouldn't have to change the ViewResolverdefinition, then.

  • You could try to have the controllers return a view name of something like ../../another.jsp, and hope that the servlet container resolves to /WEB-INF/jsp/../../another.jspto /another.jsp.

  • The ViewResolveris only consulted if the controllers return the name of a view. Your controllers don't haveto return the name of a view, they can return a Viewobject directly, in this case a JstlView. This can point to whichever JSP you like. You can some controllers returning view names, and some returning Viewobjects.

  • Remove the prefixproperty from your view resolver. This means you'd also have to change every existing controller, to prefix every view name they return with /WEB-INF/jsp/. Then you could refer to the JSPs under WebContentby name.

  • 您不能将 JSP 文件从 移动WebContent/WEB-INF/jsp/吗?那么您就不必更改ViewResolver定义。

  • 您可以尝试让控制器返回类似于 的视图名称../../another.jsp,并希望 servlet 容器解析/WEB-INF/jsp/../../another.jsp/another.jsp

  • ViewResolver如果控制器返回一个视图名称只咨询。您的控制器不具备返回一个视图的名字,他们可以返回一个View直接对象,在这种情况下JstlView。这可以指向您喜欢的任何 JSP。你可以一些控制器返回视图名称,一些返回View对象。

  • prefix从您的视图解析器中删除该属性。这意味着您还必须更改每个现有控制器,在它们返回的每个视图名称前加上/WEB-INF/jsp/. 然后您可以WebContent按名称引用 JSP 。