开发者

Spring @RequestParam doesn't handle multiple variables correctly - example test case

开发者 https://www.devze.com 2023-02-16 12:05 出处:网络
The test case given below shows a simple case where I have 2 parameters paramA and paramB. If I call the /paramtest url the paramtest() method is called.

The test case given below shows a simple case where I have 2 parameters paramA and paramB.

  • If I call the /paramtest url the paramtest() method is called.
  • If I enter true for paramA, method aTrue() is called.
  • However, when I enter true for both paramA and paramB the method bTrueNotA() is called.

But the 3rd @RequestMapping calls for A=True and B!=true. By my reconing when both paramete开发者_Go百科rs are true, aTrue() should be called.

@RequestMapping("paramtest")
@ResponseBody
public String paramtest(){
    return  "<html><head></head><body>" +
                "<form action=paramtest method=post>" +
                    "paramA: <input type=text name=paramA /><br>" +
                    "paramB: <input type=text name=paramB /><br>" +
                    "<input type=submit>" + 
                "</form>" +
            "</body></html>";       
}

@RequestMapping(value="paramtest", params="paramA=true")
@ResponseBody
public String aTrue(){
    return "A=true";
}

@RequestMapping(value="paramtest", params={"paramB=true", "paramA!=true"})
@ResponseBody
public String bTrueNotA(){
    return "B=True; A!=true";
}


I think it might be a bug in Spring. I tried with the following mappings:

@RequestMapping(value="/paramtest", params={"paramA=true"})
@ResponseBody
public String function A() { return "A"; }

@RequestMapping(value="/paramtest", params={"paramA=true", "paramB=foobar"})
@ResponseBody
public String function B() { return "B"; }

@RequestMapping(value="/paramtest", params={"paramA=!true", "paramB=foo"})
@ResponseBody
public String function C() { return "C"; }

and using your existing form with the following parameters, these were the results I got:

paramA=true A() called as expected

paramA=true, paramB=foobar B() called as expected

paramA=not_true, paramB=foo 404 page, and not C() as expected.

I got this error on the Tomcat console:

WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 142 - No matching handler method found for servlet request: path '/paramtest', method 'POST', parameters map['paramB' -> array<String>['foo'], 'paramA' -> array<String>['not_true']]

All of this with Spring 3.0.5. Note that the myParam!=myValue was only available from Spring 3.0.4 onwards (the 3.0.3 doc does not list that option). Also, I do not think that !myParam=myValue is valid, as this is not listed in the current 3.0.5 documentation.

Sorry this is not a solution to your problem but wanted to share my investigation :)

0

精彩评论

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

关注公众号