gpt4 book ai didi

java - 使用 BeanUtilsBean 将请求填充到对象中的 HashMap

转载 作者:行者123 更新时间:2023-12-01 04:50:39 25 4
gpt4 key购买 nike

我当前的应用程序使用这种函数从 http 请求填充一个简单的 java 对象:

// request is an instance of org.apache.catalina.connector.RequestFacade
// myObject is an instance of MyBusinessDTO
org.apache.commons.beanutils.BeanUtilsBean.populate( myObject, request.getParameterMap( ) );

它几乎适用于任何用例,但我仍然坚持一个,从我的请求参数填充 HashMap。

我想要填充的对象如下所示:

public class MyBusinessDTO extends BusinessDTO
{

private Map<String, Object> listeFormsChecked = new HashMap<String, Object>( );


/**
* @return the listeFormsChecked
*/
public Map<String, Object> getListeFormsChecked( )
{
return listeFormsChecked;
}



/**
* @param listeFormsChecked the listeFormsChecked to set
*/
public void setListeFormsChecked( Map<String, Object> listeFormsChecked )
{
this.listeFormsChecked = listeFormsChecked;
}
}

我已经尝试过我的请求,例如:

  • prefixPath.myBusinessDTOAttribute.key=value
  • prefixPath.myBusinessDTOAttribute[key]=值

但是它们都不起作用。

这可能吗?如果是,怎么做?

最佳答案

不是 100% 确定,我也刚刚开始接触 beanutils,但是从 javadoc 来看,您的想法似乎不受支持:

"This method uses Java reflection APIs to identify corresponding "property setter" method names, and deals with setter arguments of type String, boolean, int, long, float, and double. In addition, array setters for these types (or the corresponding primitive types) can also be identified."

所以,有数组,但没有映射。

但是你可以尝试使用两个同步(不是java意义上的)数组,并使用beanutils将它们独立地设置在单独的变量中——这样你的键将是一个具有自己的getter和setter以及val的数组将是具有自己的 getter 和 setter 的第二个数组,然后您可以在 bean 中使用一个实用方法来自动将它们合并到映射中。

关于java - 使用 BeanUtilsBean 将请求填充到对象中的 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15029639/

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