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

查看:43
本文介绍了Spring - 将一个 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>

更新

这是网址映射器.如果我添加一个控制器,我如何返回 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>

推荐答案

我认为您可以从 tuckey.org 制作的开源 URL 重写库中受益.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 重写为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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