6

我正在将旧 servlet 应用程序转换为 Spring 3.1。在此过程中,一些 URL 现在已过时。我们的网络出现了一些问题,这些问题不会很快得到解决。我的老板不想相信他们的重定向将始终有效。所以,她让我把自己的重定向放到 webapp 中。

一切都很好,除了如果 URL 有一个尾部斜杠 Spring 3.1 将找不到处理它的 Controller 类函数。

http://blah.blah.blah/acme/makedonation 被找到、映射和处理

http://blah.blah.blah/acme/makedonation / 没有

这是我用来处理旧 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({"makeadonation","contact","complain"})
    public String home() {
        logger.debug("started...");
        return "redirect:logout";

    }// end home()  

}// end class LegacyServletController

我在 Google 上四处搜索,发现这篇 Stack Overflow帖子提供了一些建议,但我是 Spring 新手,对它的理解还不够,无法实施其中的一些建议。这听起来特别适合我的需求:

spring 3.1 RequestMappingHandlerMapping 允许您设置“useTrailingSlashMatch”属性。默认情况下为真。我认为将其切换为 false 可以解决您的问题,

谁能给我一个基本的例子,引用我一个有这样一个例子的 URL(我在谷歌上没有运气)或者给我一个更好的主意?

非常感谢史蒂夫

4

2 回答 2

6

您应该在 context.xml 中配置您的 bean,并设置属性。或者您可以参考链接或弹簧文档第16.4节

示例配置

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>
于 2012-07-19T15:10:34.730 回答
2

如果您使用的是 Spring 的 Java @Configuration,您也可以@Bean像这样声明:

@Bean
public RequestMappingHandlerMapping useTrailingSlash() {
    return new RequestMappingHandlerMapping() {{ setUseTrailingSlashMatch(true); }};
}
于 2014-12-05T08:18:34.247 回答