f:selectItems seems not correctly

Splash Forums PrettyFaces Users f:selectItems seems not correctly

This topic contains 2 replies, has 2 voices, and was last updated by  xuzhike 6 years, 9 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #17832

    xuzhike
    Participant

    Hi, guys, would you please help on the following question about f:selectItems?


    list.xhtml

    <h:selectOneMenu id=”province” value=”#{userView.provinceId}”>

    <f:selectItems value=”${userView.provinces}” var=”province”

    itemLabel=”# {province.provinceName}” itemValue=”#{province.provinceId}”/>

    </h:selectOneMenu>

    userView – @Scope(“session”), has a getProvinces method:


    public List<Province> getProvinces() {

    log.info(“userView.getProvinces”);

    //return this.supportService.getAllProvince();

    return buildProvinces();

    }

    private List<Province> buildProvinces(){

    List<Province> provinces = new ArrayList<Province>();

    for(int i=0;i<3;i++){

    Province p = new Province();

    p.setProvinceName(“province – “+i);

    p.setProvinceId(i+1);

    provinces.add(p);

    }

    return provinces;

    }


    test result:

    the list.xhtml shown correctly, dropdwon list has three options.

    but per checking the log, found the result:

    23:38:56,140 INFO UserView:94 – userView.getProvinces

    23:38:56,171 INFO UserView:94 – userView.getProvinces

    twice!!!!! why? ( I think it should be once)

    I changed the userView scope, but test same.

    is it JSF implementation defect?

    I use Sun’s implementation.

    thx a lot

    Regards

    Zhike

    #20469

    Thats not necessarily a bug in the JSF implementation. Its typical for JSF that getters are called multiple times in the request lifecycle.

    Therefore you should NEVER do expensive stuff (like database queries) in your getters. Instead make sure that the data is initialized correctly by either using something like PrettyFaces page actions or use some kind of lazy initialization like this:

    @ManagedBean
    @RequestScoped
    public class ProvinceBean {

    private List<Province> provinceList = null;

    public List<Province> getProvinces() {

    if(provinceList == null) {
    provinceList = ....; // build list here
    }

    return provinceList;
    }
    }

    #20470

    xuzhike
    Participant

    thanks you, Christian. I got it.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.