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

2024-04-29

我正在将旧版 servlet 应用程序转换为 Spring 3.1。在此过程中,一些 URL 现在已过时。我们的网络存在一些问题,短期内不会得到解决。我的老板不想相信他们的重定向将始终有效。因此,她要求我将自己的重定向放入网络应用程序中。

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

http://blah.blah.blah/acme/makedonation http://blah.blah.blah/acme/makedonation被发现、绘制和处理

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 Overflowpost https://stackoverflow.com/questions/8711392/spring-mvc-urls-with-trailing-slash-redirection它提供了一些建议,但我对 Spring 很陌生,对它的理解还不够,无法实现其中一些建议。这听起来特别适合我的需求:

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

有人能给我一个基本的例子来说明如何做到这一点,给我引用一个有这样的例子的网址(我在谷歌上没有运气)或者给我指出一个更好的想法吗?

提前非常感谢 史蒂夫


你应该在 context.xml 中配置你的 bean,并设置属性。 或者你可以参考link http://forum.springsource.org/showthread.php?120192或春季文档部分16.4 http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-handlermapping

配置示例

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring-MVC 3.1:如何映射带有尾部斜杠的 URL? 的相关文章

随机推荐