开发者

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

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)

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). This has to be decided from another object

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

开发者_如何学Gowhere 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.

The question is simple: How can I do that. Any help will be highly appreciated.


By using List<> instead of Set<> in controller. Problem solved. May be Set<> has no getter/setter that can be bind with spring form.

So <form:input path="pluginParameters[${itemsRow.index}].pluginParmvalues[0].parmValue" /> and List<> in controller makes my life easier.


The Set is not an indexed collection so I could not work by using this syntax

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜