How to pass list of strings as hidden input from jsf page to fallback scope with bean request

I want to pass a list of strings as hidden input from a jsf page to a bean fallback.

Bean support is and should be the scope of the request.

Trying to do it this way, but doesn't seem to work. Any ideas how this can be done better?

<ui:repeat value="#{bean.strings}" var="#{string}">
    <h:inputHidden value="#{string}"/>
</ui:repeat>

      

+3


source to share


1 answer


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 as it is.

If you are using JSF 1.2, you just need to drop the annotation @FacesConverter

and register the converter 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>

      

+3


source







All Articles