Dale
Dale

Reputation: 1301

How/What's the best way to pass a list of Strings from jsf to backing bean

Here's how I'm doing it. There has to be a better way.. I hope.

I'm using a jsf datatable to display my data. The first column in each row is a checkbox.

Multiple checkboxes can be selected. When the submit button is hit I'm using javascript/jquery to get all of the checked boxes and get that rows ID which is a string.

In my js I'm concatenating all of the IDs into one string separating them with a comma. Then I set the value of a hidden input on my jsf/jsp page to the concatenated string. In the backing bean I extract all of those IDs from the Strings and create a List of Strings.

I'd like to be able to create the list in my javascript, pass that list of Strings to the backing bean somehow, maybe hidden input still, and avoid the string concat.

I'm using jsf1.2 if that matters.

Thanks for your suggestions. Code examples are appreciated, but not necessary.

Upvotes: 1

Views: 1066

Answers (1)

BalusC
BalusC

Reputation: 1108632

You could let JS fill a <h:inputHidden>.

<h:form id="form">
    <h:inputHidden id="ids" value="#{bean.ids}" />
    // ...

with

document.getElementById("form:ids").value = yourCommaSeparatedString;

You could create a Converter which converts a comma separated String to String[] and vice versa so that you can make ids a String[] property. You can find a basic example here: taking multiple values from inputText field separated by commas in JSF.


Unrelated to the concrete question, that's a bit hacky. Just use <h:selectBooleanCheckbox> with a Map<Long, Boolean> or something. This way you don't need to throw in any JS code. See also How to select multiple rows of <h:dataTable> with <h:selectBooleanCheckbox>.

Upvotes: 1

Related Questions