Spring-MVC 3.1:如何用斜杠映射URL?


问题内容

我正在将旧版servlet应用程序转换为Spring
3.1。在此过程中,某些URL现在已过时。我们的网络出现了一些问题,这些问题很快将无法解决。我的老板不想相信他们的重定向将一直有效。因此,她要求我将自己的重定向放入网络应用中。

所有工作都很好,除非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

但是我是Spring的新手,对它的理解还不足以实现其中的一些建议。这听起来特别适合我的需求:

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

谁能给我一个基本的例子,给我引用一个有这样的例子的URL(我对谷歌不走运)还是给我一个更好的主意?

预先感谢史蒂夫


问题答案:

您应该在context.xml中配置bean并设置属性。或者您可以参考链接或Spring
Doc第16.4

示例配置

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>