Spring @RequestParam 无法正确处理多个变量 - 示例测试用例

发布于 2024-10-21 16:13:49 字数 1291 浏览 0 评论 0原文

下面给出的测试用例显示了一个简单的情况,其中我有 2 个参数 paramAparamB

  • 如果我调用 /paramtest url,则会调用 paramtest() 方法。
  • 如果我为 paramA 输入 true,则会调用方法 aTrue()
  • 但是,当我为 paramAparamB 输入 true 时,将调用方法 bTrueNotA()

但第三个 @RequestMapping 需要 A=TrueB!=true。根据我的侦察,当两个参数都为 true 时,应该调用 aTrue()

@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";
}

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 parameters 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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2024-10-28 16:13:49

我认为这可能是 Spring 的一个错误。我尝试使用以下映射:

@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"; }

并使用带有以下参数的现有表单,这些是我得到的结果:

paramA=true A() 按预期调用

paramA=true, paramB=foobar B() 按预期调用

paramA=not_true, paramB=foo 404 页面,而不是按预期调用 C()。

我在 Tomcat 控制台上收到此错误:

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。请注意,myParam!=myValue 仅从 Spring 3.0.4 开始可用(3.0.3 doc 未列出该选项)。另外,我认为 !myParam=myValue 无效,因为它没有在 当前 3.0.5 文档

抱歉,这不能解决您的问题,但想分享我的调查:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文