users@glassfish.java.net

Re: JSF h:dataTable element seems to have a bug

From: Duncan Bloem <dabloem_at_gmail.com>
Date: Sun, 31 Jul 2011 11:07:34 +0200

Hi Dan,

It's not a bug, it's common behaviour.
You should not initialize (construct) the list in the getter method, but
preferable before the getter is accessed by h:dataTable.

May I suggest to use the ##jsf irc channel on freenode, You will get instant
reply there on jsf specific questions most of the time.

Duncan

On Sat, Jul 30, 2011 at 8:47 PM, <forums_at_java.net> wrote:

> Hello,
>
> I am building a JSF 2.0 web application using Netbeans 7.0. I think there
> is a bug in the JSF h:datatable element. Following is a complete
> application
> that illustrates the problem, consisting of an index.xhtml file and a
> backing
> bean called DataTableTestBean.
>
> If you run this application in Netbeans using Glassfish, you will see that
> the
> message "inside getDataList()" is output 3 times on the Glassfish server
> log.
> This indicates that each time the index.xhtml file is run, the
> getDataList()
> method referenced by "#{dataTableTestBean.dataList}**" is executed 3
> times,
> whereas (I believe) it should only execute once.
>
> This is problematic for me, since my application has some fairly complex
> bean
> methods, so that it is very inefficient to have them executing multiple
> times
> when only one execution is needed.
>
> Would anyone know anything about this and how to deal with it?
>
> Thanks,
>
> --Dan Schwartz
> Florida State University
>
> :::: index.html file ::::
>
> <?xml version='1.0' encoding='UTF-8' ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/**DTD/xhtml1-transitional.dtd<http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>[1]">
> <html xmlns="http://www.w3.org/1999/**xhtml <http://www.w3.org/1999/xhtml>[2]"
> xmlns:h="http://java.sun.com/**jsf/html<http://java.sun.com/jsf/html>[3]"
> xmlns:f="http://java.sun.com/**jsf/core<http://java.sun.com/jsf/core>[4]">
> <h:head>
> <title>Data Table Test</title>
> </h:head>
> <h:body>
> <h1> Data List </h1>
> <br/>
> <h:dataTable value="#{dataTableTestBean.**dataList}" var="dl">
> <h:column>
> <f:facet name="header">Data</f:facet>
> #{dl}
> </h:column>
> </h:dataTable>
> </h:body>
> </html>
>
> :::: DataTableTestBean.java file ::::
>
> package datatabletest;
>
> import javax.faces.bean.ManagedBean;
> import java.util.ArrayList;
>
> @ManagedBean(name = "dataTableTestBean")
> public class DataTableTestBean {
>
> public DataTableTestBean() {
> }
> private ArrayList<String> dataList = null;
>
> public ArrayList<String> getDataList() {
> System.out.println("inside getDataList()");
> dataList = new ArrayList<String>();
> dataList.add("Data item 1");
> dataList.add("Data item 2");
> return dataList;
> }
> }
>
>
>
> [1] http://www.w3.org/TR/xhtml1/**DTD/xhtml1-transitional.dtd<http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
> [2] http://www.w3.org/1999/xhtml
> [3] http://java.sun.com/jsf/html
> [4] http://java.sun.com/jsf/core
>
> --
>
> [Message sent by forum member 'Dan_Schwartz']
>
> View Post: http://forums.java.net/node/**828015<http://forums.java.net/node/828015>
>
>
>