繁体   English   中英

如何从Spring中的@RequestMapping中排除url映射?

[英]How to exclude url mappings from @RequestMapping in Spring?

我有一个请求映射,可以在上下文之后处理任何字符串,例如www.example.com/anystring

我按如下方式处理:

@RequestMapping(value="/{str}", method = RequestMethod.GET)
public String getApp(@PathVariable("str") String anyString, ModelMap model) {
        //Do something
    }

问题是我在我的应用程序的URL 2-3处网址如下:www.example.com/about,www.example.com/contact等。

我为他们编写了Request Mappings,如下所示:

@RequestMapping("/about")
    public String getAboutPage() {
        return "about";
    }

但很明显,因为我已经声明任何字符串应该由getApp()处理,所以getAboutPage()永远不会被执行。 如何从getApp()映射中排除/about/contact getApp() 我们显然可以在URL字符串中添加另一个关键字,但这在我的应用程序用例中是不可能的。 请帮助。 :(

编辑:

我应该只处理/about/contactgetApp()/contact如:

@RequestMapping(value="/{str}", method = RequestMethod.GET)
    public String getApp(@PathVariable("str") String anyString, ModelMap model) {

    if(anyString.equals("about")){
     //do about related stuff
    }

    if(anyString.equals("contact")){
     //do contact related stuff
    }

    //Do something
    }

有没有更好的办法?

在“catch-all”映射中指定HTTP请求方法可能使路径匹配器认为它比绝对路径映射更具体。

在绝对路径上指定请求方法,映射比较器应在包含路径变量的绝对匹配之前对绝对匹配进行排序。

例如。

@RequestMapping("/about", method = RequestMethod.GET)

或者,您可以删除catch-all上的方法规范:

@RequestMapping("/{str}")

它完全取决于您的url结构以及这些路径中的任何一个是否会接受不同的http请求方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM