Redirect SpringMVC:当原始URL以斜杠结尾时,重定向未正确更改的URL

Redirect SpringMVC:当原始URL以斜杠结尾时,重定向未正确更改的URL,redirect,spring-mvc,Redirect,Spring Mvc,我正在使用Spring3.1更新一个非常旧的基于Servlet的站点。一些URL已经过时。我的老板不相信网络维护重定向的可靠性,所以她让我把自己的重定向从过时的URL放到webapp中 我制作了一个名为LegacyServletController的控制器来处理过时的URL。除非有人在URL上键入一个尾随斜杠,否则它非常有效。控制器方法仍然会拾取它,但它不会重定向到新的URL。它只是将新的URL添加到位置栏中已有的URL中 例如,这是一个过时的URL: <beans xmlns="http

我正在使用Spring3.1更新一个非常旧的基于Servlet的站点。一些URL已经过时。我的老板不相信网络维护重定向的可靠性,所以她让我把自己的重定向从过时的URL放到webapp中

我制作了一个名为LegacyServletController的控制器来处理过时的URL。除非有人在URL上键入一个尾随斜杠,否则它非常有效。控制器方法仍然会拾取它,但它不会重定向到新的URL。它只是将新的URL添加到位置栏中已有的URL中

例如,这是一个过时的URL:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.acme.controller" />

  <mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/acme/common/,file:/c:/ftp/acme/"/>
  <mvc:annotation-driven/>

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/jsp/"/>
    <property name = "suffix" value = ".jsp"/>
  </bean>

  <bean name="af" class="com.acme.controller.security.CustomAuthenticationFilter"/>

</beans>

我希望它重定向到

但是,当过时的URL具有如上所述的尾随斜杠时,这就是重定向产生的结果:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.acme.controller" />

  <mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/acme/common/,file:/c:/ftp/acme/"/>
  <mvc:annotation-driven/>

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/jsp/"/>
    <property name = "suffix" value = ".jsp"/>
  </bean>

  <bean name="af" class="com.acme.controller.security.CustomAuthenticationFilter"/>

</beans>

我猜我需要在我的*-servlet.xml中使用另一个URL处理程序,但我对Spring还是新手,不知道如何设置,以便我的控制器函数正确地处理带有或不带有斜杠的过时URL

下面是我用来处理遗留URL的控制器类

import org.springframework.stereotype.Controller;
import org.springframework.validation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;


import org.apache.log4j.Logger;

@Controller
public class LegacyServletController {

    private static final Logger logger = Logger.getLogger(LegacyServletController.class);

    // Redirect these legacy screns "home", the login screen via the logout process
    @RequestMapping({"moreinfo","other_dead_screen"})
    public String home() {
        logger.debug("started...");
        return "redirect:home";

    }// end home()  

}// end class LegacyServletController
这是我的acme servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.acme.controller" />

  <mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/acme/common/,file:/c:/ftp/acme/"/>
  <mvc:annotation-driven/>

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/jsp/"/>
    <property name = "suffix" value = ".jsp"/>
  </bean>

  <bean name="af" class="com.acme.controller.security.CustomAuthenticationFilter"/>

</beans>


最好为重定向提供完整路径,理想情况下,您可以返回
重定向:/home
,这将重定向到相对于web应用程序根目录的路径-
http://blah.blah.blah/acme/home

最好为重定向提供完整路径,理想情况下,您可以返回
重定向:/home
,这将重定向到相对于web应用程序根目录的路径-
http://blah.blah.blah/acme/home

返回语句应该是:
return“redirect:/home”
。控制器应返回视图解析器的绝对路径


这个问题的答案也很有用:

返回语句应该是:
return“redirect:/home”
。控制器应返回视图解析器的绝对路径

这个问题的答案也很有帮助: