Just use a converter for the list value:
<h:inputHidden value="#{bean.strings}" converter="myStringListConverter" />
Here is a converter that converts to/from a String using @@@ as separator:
@FacesConverter("myStringListConverter")
public class StringListConverter implements Converter {
// this is used as a regex, so choose other separator carefully
private static final String MY_SEPARATOR = "@@@";
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
if (value == null) {
return new ArrayList<String>();
}
return new ArrayList<String>(Arrays.asList(value.split(MY_SEPARATOR)));
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null) {
return "";
}
return join((List<String>) value, MY_SEPARATOR);
}
/**
* Joins a String list, src: http://stackoverflow.com/q/1751844/149872
*
* @param list
* @param conjunction
* @return
*/
public static String join(List<String> list, String conjunction) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String item : list) {
if (first) {
first = false;
} else {
sb.append(conjunction);
}
sb.append(item);
}
return sb.toString();
}
}
If you are using JSF 2, this should work for you already as it is.
In case you are using JSF 1.2, you just have to drop the @FacesConverter
annotation and register the converter in the faces-config.xml
like so:
<converter>
<description>Simple String List Converer</description>
<converter-id>myStringListConverter</converter-id>
<converter-class>com.your.package.StringListConverter</converter-class>
</converter>