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

Ste*_*eve 2 redirect spring-mvc

我正在使用 Spring 3.1 来翻新一个非常旧的基于 Servlet 的网站。某些 URL 已过时。我的老板不信任网络维护重定向的可靠性,因此她要求我将自己的重定向从过时的 URL 移至 Web 应用程序中。

我制作了一个名为LegacyServletController 的控制器来处理过时的 URL。除非有人在 URL 上输入尾部斜杠,否则它会很好用。Controller 方法仍然会获取它,但不会重定向到新的 URL。它只是将新 URL 添加到地址栏中已有的 URL 中。

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

http://blah.blah.blah/acme/moreinfo/

我希望它重定向到

http://blah.blah.blah/acme/home

但是,当过时的 URL 具有如上所述的尾部斜杠时,重定向会产生以下结果:

http://blah.blah.blah/acme/moreinfo/home

我猜我的 *-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
Run Code Online (Sandbox Code Playgroud)

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

Lou*_*uis 5

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

这个问题的答案也很有帮助:redirect in Spring MVC