Spring - 将一个URL重写为另一个URL [英] Spring - Rewrite one URL to another

查看:86
本文介绍了Spring - 将一个URL重写为另一个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 .html I 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?

我的项目如下:

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

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

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

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.

在我的web.xml中,我有

In my web.xml, I have

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

更新

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

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.

<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>


推荐答案

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

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.

请参见这里的主页。 Skaffman是对的,你希望它转发而不是重定向,这是默认行为。

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

在web.xml中配置它如下:

Configure it in web.xml like this:

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

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

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

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

这篇关于Spring - 将一个URL重写为另一个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆