Spring - Rewrite one URL to another

后端 未结 3 564
花落未央
花落未央 2020-12-09 14:01

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

相关标签:
3条回答
  • 2020-12-09 14:02

    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.

    0 讨论(0)
  • 2020-12-09 14:07

    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

    0 讨论(0)
  • 2020-12-09 14:10

    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>
    
    0 讨论(0)
提交回复
热议问题