简体   繁体   中英

JSF f:selectItems using List error

Facelet code:

<h:selectOneMenu id = "country" label = "country" value = "#{beanController.countryResidence}">
    <f:selectItems value = "#{countries.countries}" />
</h:selectOneMenu>

Bean Code:

@ManagedBean(eager=true, name = "countries")
@ApplicationScoped
public class CountriesConstants {
        private List<SelectItem> countries;
        public CountriesConstants(){
            countries.add(new SelectItem("DE", "Germany"));
            countries.add(new SelectItem("JA", "Japan"));
            countries.add(new SelectItem("RU", "Russia"));
            countries.add(new SelectItem("US", "United States"));
        }
        public List<SelectItem> getCountries() {
            return countries;
        }
        public void setCountries(List<SelectItem> countries) {
            this.countries = countries;
        }
}

The Error

SEVERE: Exception while loading the app

SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.RuntimeException: com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.mysite.util.CountriesConstants.

I followed some tutorials step by step but I keep getting this error. I tried making the List static and initialise the values in a static block but I get the same error.

EDIT:

The new Bean Code

@ManagedBean(eager=true, name="constants")
@ApplicationScoped
public class Constants {

    public static final String VALIDATE_DETAILED = "detailed";
    public static final List<SelectItem> countries;

    static{
        countries = new ArrayList<SelectItem>();
        countries.add(new SelectItem("DE", "Germany"));
        countries.add(new SelectItem("JA", "Japan"));
        countries.add(new SelectItem("RU", "Russia"));
        countries.add(new SelectItem("US", "United States"));
    }

    public List<SelectItem> getCountries() {
        return countries;
    }
}

This seems to work but I find it weird that I can access a static attribute with a non static method. If I remove the getCOuntries() method an error saying that no countries attribute exists is thrown.

In your bean constructor, you must create your List first, try this:

public CountriesConstants(){
   countries = new LinkedList<SelectItem>();
   countries.add(new SelectItem("DE", "Germany"));
   countries.add(new SelectItem("JA", "Japan"));
   countries.add(new SelectItem("RU", "Russia"));
   countries.add(new SelectItem("US", "United States"));
}

Besides, your <f:selectItems> tag should have more attribute. Something like this:

<f:selectItems value="#{countries.countries}" var="c" itemLabel="#{c.name}" itemValue="#{c.id}" />

UPDATE : suppose you have the following controller

@ManagedBean
@RequestScoped
public class BeanController {
   private String countryResidence;
}

Initialize your arrayList first

private List<SelectItem> countries = new ArrayList<SelectItem>();

Your facelets code seems fine .

private final List<SelectItem> countries = new ArrayList<SelectItem>();

Initialize and declare List as "final" if you want to make the countries object not be instantiated again. It is a good practice to use final which improves readability of the code as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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