2012-07-19 79 views
6

我将旧的servlet应用程序转换为Spring 3.1。在这个过程中,一些URL现在已经过时了。我们的网络出现了一些问题,无法马上解决。我的老板不想相信他们的重定向将始终运作。所以,她让我把自己的重定向到webapp中。Spring-MVC 3.1:如何映射带有斜线的URL?

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

http://blah.blah.blah/acme/makedonation被发现,映射和处理

http://blah.blah.blah/acme/makedonation/不

这里是我使用来处理我的旧有的网址

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搜索了一圈,发现控制器类这个堆栈溢出post,提供了几个建议,但我是新来的Spring,并没有足够的理解它来实现一些suggestio纳秒。这其中尤其是听起来像这将是非常适合我的需求:

春季3.1 RequestMappingHandlerMapping允许您设置一个 “useTrailingSlashMatch”属性。默认情况下它是真的。我认为 它转换为false将解决您的问题,

会有人给我如何说,引用我说,有这样一个例子的URL一个基本的例子(我曾与谷歌没有运气)或点我更好的主意?

感谢很多提前 史蒂夫

+0

我我看到了这样的解决方案'@RequestMapping(value = {“/ search /”,“/ search”},method = RequestMethod.GET)' – basil 2016-08-08 06:29:14

回答

6

你应该配置在context.xml中你的bean,并设置属性。 或者你可以参考link或弹簧DOC部分16.4

示例配置

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

你的意思是我的* -servlet.xml还是我的web.xml? – Steve 2012-07-19 15:40:58

+0

我的意思是* -servlet.xml – 2012-07-19 15:45:10

2

如果您正在使用Spring的Java的@Configuration你也可以只申报一个@Bean这样的:

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

如果您有,那不会起作用。这是你如何解决它:http://stackoverflow.com/questions/15912329/exact-requestmapping-with-spring – 2016-04-17 09:53:31