Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'd need to provide the whole <code>Language</code> objects as both the available items and the selected items. You also need to create a <code>Converter</code> which converts between the <code>Language</code> object and <code>String</code>, this is mandatory because HTML output and HTTP request parameters are one and all <code>String</code>.</p> <p>Assuming that your <code>Language</code> object has two properties <code>code</code> and <code>name</code> and that you've an application scoped bean which look like this:</p> <pre><code>@ManagedBean @ApplicationScoped public class Data { private List&lt;Language&gt; languages; @PostConstruct public void init() { languages= new ArrayList&lt;Language&gt;(); languages.add(new Language("en", "English")); languages.add(new Language("hi", "Hindi")); languages.add(new Language("te", "Telugu")); // ... } public List&lt;Language&gt; getLanguages() { return languages; } } </code></pre> <p>Then you can use it as follows:</p> <pre><code>&lt;h:selectManyCheckbox value="#{bean.selectedLanguages}" converter="languageConverter"&gt; &lt;f:selectItems value="#{data.languages}" var="language" itemValue="#{language}" itemLabel="#{language.name}" /&gt; &lt;/h:selectManyCheckbox&gt; </code></pre> <p>with this bean</p> <pre><code>@ManagedBean @ViewScoped public class Bean { private List&lt;Language&gt; selectedLanguages; // ... } </code></pre> <p>and this converter</p> <pre><code>@FacesConverter("languageConverter") public class LanguageConverter implements Converter { @Override public String getAsString(FacesContext context, UIComponent component, Object object) { return ((Language) object).getCode(); } @Override public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) { List&lt;Language&gt; languages = (List&lt;Language&gt;) context.getApplication().evaluateExpressionGet(context.getELContext(), "#{data.languages}", List.class); for (Language language : languages) { if (language.getCode().equals(submittedValue)) { return language; } } return null; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload