开发者

Did jsf <h:datatable> fetch data two times? [duplicate]

开发者 https://www.devze.com 2023-02-19 05:04 出处:网络
This question already has answers here: Why JSF calls getters multiple times (9 answers) Closed 5 years ago.
This question already has answers here: Why JSF calls getters multiple times (9 answers) Closed 5 years ago.

I'm using JSF 2.0. I wanted to use h:dataTable and I realized that the function from value in dataTable is invoked 2 times when I open view in browser.

I'm confused with that and I don't know what I'm doing wrong. Maybe it's normal behaviour(?). Please help.

It will go to System.out.println("getList"); 2 times. It is right?

开发者_开发知识库

Here is my code.

Bean class:

package com.sonicwall.es.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class UserBean {

private List<String> list=new ArrayList<String>();

public UserBean() {
    list.add("Tom");
    list.add("Andy");
}

public List<String> getList() {
    System.out.println("getList");
    return list;
}

public String getString() {
    System.out.println("getString");
    return "String";
}

}

View

string:

 <h:outputText value="#{userBean.string}" />    

list:

<h:dataTable cellpadding="10" cellspacing="0" border="5"
    value="#{userBean.list}" var="item">
  <h:column>        
    <h:outputText value="#{item}">
    </h:outputText>         
  </h:column>   
</h:dataTable>      


Getters can be called several times during JSF lifecycle. I think these answers to a similar question can help you. This comprehensive tutorial about jsf lifecycle is also useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号