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

发布于 2024-08-22 10:47:21 字数 1499 浏览 8 评论 0原文

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

我的项目如下所示:

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,它们必须继续提供现有的页面。

在我的 web.xml 中,我有

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

更新

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

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?

My project looks like:

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

I want localhost:8080/offers.html to redirect to 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.

In my web.xml, I have

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-08-29 10:47:21

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

请参阅此处查看其主页。 Skaffman 是对的,你希望它“转发”而不是重定向,这是默认行为。

在 web.xml 中配置如下:

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

然后,在 WEB-INF/urlrewrite.xml 中有一个如下元素:

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

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.

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

Configure it in web.xml like this:

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

<rule>
    <from>offers.html</from>
    <to>offers.jsp</to>     
</rule>
无声情话 2024-08-29 10:47:21

我将使用 OCPsoft PrettyFaces 或 OCPsoft Rewrite 来实现此目的:

使用 PrettyFaces

创建 WEB- INF/pretty-config.xml

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

使用重写

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

我希望这会有所帮助。

~林肯

I would use OCPsoft PrettyFaces or OCPsoft Rewrite for this:

With PrettyFaces:

create 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

爱你不解释 2024-08-29 10:47:21

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

鉴于此,您可以尝试以下一些操作:

  • 您不能将 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。

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

SO given that, here are some things to try:

  • Can't you just move the JSP files from WebContent into /WEB-INF/jsp/? You wouldn't have to change the ViewResolver definition, 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.jsp to /another.jsp.

  • The ViewResolver is only consulted if the controllers return the name of a view. Your controllers don't have to return the name of a view, they can return a View object directly, in this case a JstlView. This can point to whichever JSP you like. You can some controllers returning view names, and some returning View objects.

  • Remove the prefix property 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 WebContent by name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文