Spring - Rewrite one URL to another
Asked Answered
P

3

6

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>
Puritanism answered 25/2, 2010 at 0:1 Comment(2)
UrlFilenameViewController will only resolve a pattern of a URL to a file name, you can't "map" the url to a different file. If you want to map /offers.html to a view sample.jsp you'll need a controller which handles the /offers.html pattern and returns sample.jsp as the view. Can you post your SimpleUrlHandlerMapping from myapp-servlet.xml?Gam
@Rob Beardow - Updated with the SimpleUrlHandlerMapping and the InternalResourceViewResolverPuritanism
K
16

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>
Kianakiang answered 25/2, 2010 at 9:16 Comment(1)
@mirror303 - Thanks. This worked perfectly and took about 10 seconds to configure.Puritanism
H
5

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

Haitian answered 27/10, 2011 at 5:24 Comment(2)
You can also use your existing Tuckey configuration with Rewrite via the rewrite-integration-tuckey module.Haitian
Lincoln is founder of OCPsoft, the author of PrettyFaces and Rewrite. You should mention your answer is biased.Sarver
L
1

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.

Lightweight answered 25/2, 2010 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.