简体   繁体   English

如何/从jsf将字符串列表传递到支持bean的最佳方法是什么

[英]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. 我正在使用jsf数据表来显示我的数据。 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. 当点击提交按钮时,我正在使用javascript / jquery来获取所有复选框,并获取该行ID(它是一个字符串)。

In my js I'm concatenating all of the IDs into one string separating them with a comma. 在我的js中,我将所有ID串联为一个字符串,并用逗号将它们分隔开。 Then I set the value of a hidden input on my jsf/jsp page to the concatenated string. 然后,我在jsf / jsp页面上将隐藏输入的值设置为连接的字符串。 In the backing bean I extract all of those IDs from the Strings and create a List of Strings. 在支持bean中,我从字符串中提取所有这些ID,并创建一个字符串列表。

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. 我希望能够在JavaScript中创建列表,以某种方式将字符串列表传递给支持bean,也许仍然隐藏输入,并避免使用字符串concat。

I'm using jsf1.2 if that matters. 如果这很重要,我正在使用jsf1.2。

Thanks for your suggestions. 感谢您的建议。 Code examples are appreciated, but not necessary. 代码示例值得赞赏,但不是必需的。

You could let JS fill a <h:inputHidden> . 您可以让JS填充<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. 您可以创建一个Converter ,将逗号分隔的String转换为String[] ,反之亦然,以便将idsString[]属性。 You can find a basic example here: taking multiple values from inputText field separated by commas in JSF . 您可以在此处找到一个基本示例: 在JSF中,从inputText字段获取多个值,并用逗号分隔


Unrelated to the concrete question, that's a bit hacky. 具体问题无关 ,这有点棘手。 Just use <h:selectBooleanCheckbox> with a Map<Long, Boolean> or something. 只需将<h:selectBooleanCheckbox>Map<Long, Boolean> This way you don't need to throw in any JS code. 这样,您无需抛出任何JS代码。 See also How to select multiple rows of <h:dataTable> with <h:selectBooleanCheckbox> . 另请参阅如何使用<h:selectBooleanCheckbox>选择<h:dataTable>的多行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM