【问题标题】:Negating params in a Spring controller's RequestMapping在 Spring 控制器的 RequestMapping 中否定参数
【发布时间】:2011-07-11 23:16:44
【问题描述】:

你好,

我的控制器中有两种方法,如果参数是某个值,我试图让一种方法触发,如果参数不是那个值,我会尝试另一种方法。当参数等于某个值时,我的“not”方法正在触发。

根据(我对)this 的理解,我正在正确地编写表达式。以下是这两种方法的签名:

@RequestMapping(value = "/doit", params= {"output=grouped", "display!=1"})
public @ResponseBody HashMap<String, Object> doSomething(
@RequestParam("text") String text,
@RequestParam("display") String display)
{
  // this method runs if display=1 but why?
}


@RequestMapping(value = "/doit", params= {"output=grouped", "display=1"})
public @ResponseBody HashMap<String, Object> doSomethingElse(
@RequestParam("text") String text)
{
  // this method is not being called when display=1...why not?
}

我启用了 Spring 调试日志记录,我看到 Spring 将参数转换为值为“1”的 RequestParam 字符串的位置。但是,在下一行中,它决定将其映射到错误的方法。

我做错了什么?

谢谢!

【问题讨论】:

    标签: spring spring-mvc controller


    【解决方案1】:

    这不是你的错:这是一个错误SPR-8059。目前它已在 3.1M2 版和svn Trunk rev 4408 中修复。

    解决方法是不要在@RequestMapping 中使用!=。相反,您必须手动使用if

    @RequestMapping(value = "/doit", params= {"output=grouped")
    public @ResponseBody HashMap<String, Object> acceptAll(
       @RequestParam("text") String text,
       @RequestParam("display") String display) {
      if("1".equals(display) {
          return doSomethingElse(text);
      } else {
          return doSomething(text,display);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2012-01-10
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2014-01-02
      相关资源
      最近更新 更多