Spring-MVC 3.1:如何映射带有尾随斜杠的URL?

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

我在谷歌上搜索到了这篇Stack Overflow post,里面提供了几个建议,但我对Spring还不够了解,无法实现其中一些建议。特别是这个建议似乎非常符合我的需求:

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

请问是否有人能给我一个基本示例,展示如何实现此功能,或者引用一个具有此类示例的URL(我在谷歌上没有找到),或者指点我更好的想法?
非常感谢 Steve

我看到过这样的解决方案 @RequestMapping(value = {"/search/", "/search"}, method = RequestMethod.GET) - basil
2个回答

6
你需要在context.xml中配置bean,并设置属性。 或者你可以参考链接或spring文档第16.4节。 示例配置。
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>

你是指我的 *-servlet.xml 还是我的 web.xml 文件? - Steve

2

如果您正在使用Spring的Java @Configuration,您也可以像这样声明一个@Bean

@Bean
public RequestMappingHandlerMapping useTrailingSlash() {
    return new RequestMappingHandlerMapping() {{ setUseTrailingSlashMatch(true); }};
}

如果你有mvc:annotation-driven/,那不会起作用。这是如何修复的:https://dev59.com/m3DYa4cB1Zd3GeqPDrxs - Christian Nilsson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接