PDA

View Full Version : Array bind


javiere
Apr 25th, 2007, 03:14 PM
Hi all.
I need to bind a list (SELECT tag) with an array on my command object.
public class MyCommand {
String[] values;
public String[] getValues() {
return this.values;
}

public void setValues(String[] values) {
this.values = values;
}
}


Now, in my JSP, I have a SELECT (list) with add/remove options, but how do I bind all added options to my "values" in my command object?
I want that every item added to the select (every item on my list on html) would be added to "values" on MyCommand instance... Is that possible?

Thanks in advance.

geggle
May 1st, 2007, 08:56 AM
I'm just a little unclear on what exactly you want your select list to do... can you post the JSP code so we can get a better idea of what you have in the UI?

javiere
May 2nd, 2007, 08:36 AM
Here is the code:

Command class
public class SomeCommand {
private String[] values;

public String[] getValues() {
...
}
public void getValues(String[] values) {
...
}
}

jsp code:
<form ...>
...
Here I have code to add and remove items in the select below.
...
...
<spring:bind path="command.values">
<select name="values" size="7" multiple="multiple">
<c:forEach var="t" items="${status.value}">
<option value="<c:out value='${t}'/>"><c:out value="${t}"/></option>
</c:forEach>
</select>
</spring:bind>
...
...
<input type="submit" value="Update" onclick="selectAllItems(this.form.tickets);"/>
</form>

The binding is done correctly (as I want to include all items in the select "values", a javascript selects all before submitting the form).
But when the list if empty, the binding doesn't empty the values array on the command. Anyways, I solve it by adding a hidden field with a count of elements... If the count is 0, I MANUALLY clear the array.
So at the moment everything is working fine, but the code sucks.

geggle
May 2nd, 2007, 12:48 PM
Hi,

I'm not 100% sure, but I think you are missing a required hidden parameter. It is needed to "fix" the same problem (empty submit) as for checkboxes.

Add the following:
<input type="hidden" name="_${status.expression}" value="1"/>

Alternatively, may I suggest that you look into Spring's <form:*.../> tag library? If you use it, you'd do something like the following:

<form:form commandName="command" ...>
...
<form:select path="values" multiple="true">
<form:options items="${command.values}"/>
</form:select>
</form:form>

Which would automatically add the required hidden input field.

Tony.

javiere
May 2nd, 2007, 01:23 PM
Thanks Tony, I'll use the "form:" tags from Spring.