0

I am trying to render a few check boxes by using f:selectItems but web page is just not displaying check boxes as expected. Please look at the code below and let me know that what I am missing.

HTML:

    <h:form  prependId="false">
        <table align="left" cellspacing="5">
            <tr>
                <td align="right" valign="top"><h:outputText value="#{msgs.interests}" /></td>
                <td align="left" valign="top">
                    <h:selectManyCheckbox value="#{testBean.interests}">
                        <f:selectItems value="#{testBean.checkBoxItems}" />
                    </h:selectManyCheckbox>
                </td>
            </tr>
            <tr>
                <td align="right" valign="top"><h:commandButton value="#{msgs.save}" /></td>
                <td align="left" valign="top"><h:commandButton value="#{msgs.cancel}" /></td>
            </tr>
        </table>
    </h:form>

Managed Bean:

package com.jsf.ManagedBeans;

import javax.inject.Named; import javax.faces.bean.RequestScoped;

import javax.faces.model.SelectItem;

@Named("testBean") @RequestScoped public class TestBean {

private int[] interests;
private SelectItem[] checkBoxItems = {
    new SelectItem("Dancing", "Dancing"),
    new SelectItem("Singing", "Singing"),
    new SelectItem("Reading", "Reading"),
    new SelectItem("Writing", "Writing")
};

public SelectItem[] getCheckBoxItems() {

    return checkBoxItems; 
}

public int[] getInterests() {     return interests;
}
public void setInterests(int[] newValue) { 
    interests = newValue; 
} }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Akash
  • 179
  • 1
  • 3
  • 12
  • can you use `@ManagedBean` instead of `@Named`, by importing specifically from `javax.faces.bean.ManagedBean`? it works for me that way. – tt_emrah Aug 14 '14 at 09:07
  • @tt_emrah Thanks for your help. By following your comment, I tired my code with `@ManagedBean` instead of `@Named`and check boxes are appearing in web view. Now the question is why this code is not working with `@Named` CDI bean. I am searching for this now and update this thread when I will have answer. – Akash Aug 14 '14 at 12:03
  • btw, `@Named` also works with javax.faces-2.2.7.jar and weld-servlet-2.2.0.Final.jar. fyi... – tt_emrah Aug 14 '14 at 13:06
  • 1
    It's not working because you're mixing annotations: ` javax.faces.bean.RequestScoped` for JSF scoping and `javax.inject.Named` for CDI naming. You should use consistent annotations for both naming and scoping. Either pick JSF-only naming and scoping annotations or CDI-only, never mix – kolossus Aug 14 '14 at 14:17
  • @kolossus: it might be wrong conceptually, but i can tell that it's working when i use `javax.faces.bean.RequestScoped` and `javax.inject.Named` together. – tt_emrah Aug 14 '14 at 14:25
  • @tt_emrah - JSF2.2 has gone to great lengths to make the two domains interoperable, but "works" is relative. For example, `@Named` automatically gives the bean `@NoneScoped` (or the CDI equivalent). That is enough for it to run properly. It might not necessarily mean that it's the `@RequestScoped` annotation that's giving it the scoping – kolossus Aug 14 '14 at 14:31

1 Answers1

1

You'll want to use CDI scope annotations with the CDI @Named annotation and also make sure CDI is enabled for the project (WEB-INF/beans.xml file exists).

Aside from that, add a debug statement to a PostConstruct method to confirm that the bean is being created. That method is run automatically by CDI after a bean is constructed. It can also be used to initialize bean fields as it is guaranteed to run once and only once per instantiation, unlike the class constructor.

package com.jsf.ManagedBeans;

import javax.inject.Named; 
import javax.enterprise.context.RequestScoped;

import javax.faces.model.SelectItem;

@Named @RequestScoped public class TestBean {

  private int[] interests;
  private SelectItem[] checkBoxItems = {
    new SelectItem("Dancing", "Dancing"),
    new SelectItem("Singing", "Singing"),
    new SelectItem("Reading", "Reading"),
    new SelectItem("Writing", "Writing")
  };

  @PostConstruct public void init() {
      System.out.println("testBean initialized");
  }

  public SelectItem[] getCheckBoxItems() {

    return checkBoxItems; 
  }

  public int[] getInterests() {     return interests;
  }
  public void setInterests(int[] newValue) { 
    interests = newValue; 
  }   
}
Ali Cheaito
  • 3,746
  • 3
  • 25
  • 30