简体   繁体   English

如何绑定集合 <CustomObject> 以春季MVC形式

[英]How to bind a Set<CustomObject> in spring mvc form

I have a command object associated with a spring form controller: 我有一个与弹簧形式控制器关联的命令对象:

public class PluginInstance {
  private Set<PluginParameter> pluginParameters = new HashSet<PluginParameter>();
  ... some other string/long properties and getter setters...
}

the PluginParameter also have a Set in it which contain the values PluginParameter也有一个Set,其中包含值

public class PluginParameter {
  private String parmName;
  private Set<PluginParmvalue> pluginParmvalues = new HashSet<PluginParmvalue>();
  ...some other string/long properties and getter setters...
}

(Normally the pluginParmvalues will contain only one value, a list have been used for future expandability) (通常,pluginParmvalues仅包含一个值,已使用列表进行将来的扩展)

In the spring form I binding the values as 在春季形式中,我将值绑定为

<form:input path="pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue" />

but the thing is that there can be a form:select(to present multiple predefined options to the user) or form:input (user can input any value). 但问题是可以有一个form:select(向用户显示多个预定义选项)或form:input(用户可以输入任何值)。 This has to be decided from another object 这必须由另一个对象决定

public class PluginConfigParm {
  private String parmName;
  private ArrayList<String> choices;
  ...getter setters and other properties
}

where I have to compare the name of PluginConfigParm.paramName with PluginParameter.paramName when they match and PluginConfigParm.choices.size() > 0 then form:select will be shown populated with the values from PluginConfigParm.choices otherwise form:input will be shown. 当它们匹配并且PluginConfigParm.choices.size()> 0时,我必须将PluginConfigParm.paramName的名称与PluginParameter.paramName进行比较,然后form:select将显示为PluginConfigParm.choices的值填充,否则form:input将显示。

The question is simple: How can I do that. 问题很简单:我该怎么做。 Any help will be highly appreciated. 任何帮助将不胜感激。

By using List<> instead of Set<> in controller. 通过在控制器中使用List <>而不是Set <>。 Problem solved. 问题解决了。 May be Set<> has no getter/setter that can be bind with spring form. 可能是Set <>没有可以与spring形式绑定的getter / setter。

So <form:input path="pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue" /> and List<> in controller makes my life easier. 因此, <form:input path="pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue" />和List <>使我的生活更加轻松。

该集合不是索引集合,因此无法使用此语法进行工作

pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue

eg: 例如:

class person{
set name<string> = new HashSet<String>()
}


<input type="hidden" path="person.name" name="person.name" value="<%=valueStr%>"/>
take valueStr = "hello, world"

by giving it as comma seperated values.. set works 通过给它作为逗号分隔的值。

Its working for me 它为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM