gpt4 book ai didi

java - 如何有效地拆分 ServletRequest .getParameterMap()? [Spring MVC,ServletRequest]

转载 作者:行者123 更新时间:2023-11-29 03:10:24 26 4
gpt4 key购买 nike

今天我想给大家提出的问题是这样的:

请求到达 Controller (在 spring MVC 环境中),在该 Controller 中我想以某种方式拆分请求参数。我最初的方法是使用@ModelAttribute 注释

public String processForm(@ModelAttribute Mouse tom, @ModelAttribute Mouse jerry)

但是使用这种方法我将如何获得其他参数?这是有效的吗?

所以我想做这样的事情:

Mouse jerry = new Mouse();
BeanUtils.populate(jerry, request.getParameterMap());
//do something to remove the mice :) how?
Cat tom = new Cat();
BeanUtils.populate(cat, request.getParameterMap());
//do something to remove the cats how?
BeanUtils.populate(theRest, request.getParameterMap());

最终的问题是:如何通过尽可能少地遍历列表来有效地将请求拆分为 3 个不同的实体?

感谢您阅读本文,并希望得到答复。

最佳答案

我认为您是在问如何在处理程序方法中处理多个对象,而不是实际拆分表单/查询参数,是吗?

只需使用一个模型。将 Tom、Jerry 和 theRest 包装在一个新对象中:

class Foo {
Cat tom;
Mouse jerry;
Bar theRest;
...
}

public String processForm(@ModelAttribute Foo foo)

Spring MVC 可以 data bind为你。你不需要 BeanUtils。

关于java - 如何有效地拆分 ServletRequest .getParameterMap()? [Spring MVC,ServletRequest],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29679922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com