开发者

Negating params in a Spring controller's RequestMapping

开发者 https://www.devze.com 2023-03-19 18:47 出处:网络
Hi, I\'ve got two methods in my controller and I\'m trying to get one method to fire if a parameter is a certain value and the other method if the parameter is not that value.My \"not\" method is fi

Hi,

I've got two methods in my controller and I'm trying to get one method to fire if a parameter is a certain value and the other method if the parameter is not that value. My "not" method is firing when the param equals the certain value.

According to (my understanding of) this I'm writing the expression correctly. Here are the signatures of the two methods:

@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(
@RequestPar开发者_开发问答am("text") String text)
{
  // this method is not being called when display=1...why not?
}

I have Spring debug logging enabled and I see where Spring converts the parameter to a RequestParam String with value '1'. In the very next line, though, it decides to map it to the wrong method.

What am I doing wrong?

Thanks!


It is not your fault: it is a Bug SPR-8059. At the moment it is fixed in Version 3.1M2 and svn Trunk rev 4408.

A workaround would be not to use != in @RequestMapping. Instead you have to do it with an if by hand:

@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);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号