我正在将一个传统的servlet应用程序转换为Spring 3.1。在此过程中,一些URL现在已经过时。我们的网络出了一些问题,短期内不会得到解决。我的老板不想相信他们的重定向永远都能正常工作。所以,她要求我把自己的重定向放到webapp中。
所有东西都很好,除了如果一个URL有一个尾随斜杠,Spring 3.1将无法找到处理它的Controller类函数。 http://blah.blah.blah/acme/makedonation被找到、映射和处理 http://blah.blah.blah/acme/makedonation/ 不行 这是我用来处理旧URL的控制器类。
我在谷歌上搜索到了这篇Stack Overflow post,里面提供了几个建议,但我对Spring还不够了解,无法实现其中一些建议。特别是这个建议似乎非常符合我的需求:
非常感谢 Steve
所有东西都很好,除了如果一个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还不够了解,无法实现其中一些建议。特别是这个建议似乎非常符合我的需求:
请问是否有人能给我一个基本示例,展示如何实现此功能,或者引用一个具有此类示例的URL(我在谷歌上没有找到),或者指点我更好的想法?spring 3.1 RequestMappingHandlerMapping允许您设置"useTrailingSlashMatch"属性。默认情况下为true。我认为将其切换为false可以解决您的问题。
非常感谢 Steve
@RequestMapping(value = {"/search/", "/search"}, method = RequestMethod.GET)
。 - basil