CommandButton, DataTable, BackingBean, Getter will fired twice...

[nobr]Hi hackers

I got follow strange behavior and I will appreciate any comments, tips or workarounds:

I got a BackingBean to fill a DataTable. Inside the Table and on every row there is a hidden CommandButton. When User clicks anywhere on a row (TR), the onClick listener of the Row will fire the click handler of the Button. This cause the form to submit and show the detail page of the row in a different Frame. Everything works fine, however, the "getInstances" method of the backing bean will be fired every time a hit on a row. I don't want this behavior because it cause more MYSQL activity.

Here ist he JSF page:

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>

<f:loadBundle var="labels" basename="labels.domiciles" />

<html>

<head>

<%@ include file="/inc/head.jsp"%>

<title>NMA Repository - Instance > List</title>

<SCRIPT type="text/javascript">

function clear_list(){return;}

</SCRIPT>

</head>

<body class="list">

<f:view>

<t:div styleClass="pageTitleSmall">

<f:verbatim> </f:verbatim>

<t:outputText value="#{InstanceList.domicileFilter} > #{InstanceList.versionFilter} > Instances > List"></t:outputText>

</t:div>

<t:div styleClass="linksContainer">

<h:form style="float:right;">

<t:commandButton value="reload" styleClass="button" action="gotoInstanceList" id="reloadButton" forceId="true">

<t:updateActionListener value="#{InstanceList.versionFilter}" property="#{InstanceList.versionFilter}"></t:updateActionListener>

</t:commandButton>

</h:form>

<h:form target="form" style="float: right;">

<t:commandButton value="new instance" styleClass="button" action="gotoInstanceInsertForm">

<t:updateActionListener value="#{InstanceList.domicileFilter}" property="#{InstanceForm.domicileFilter}"></t:updateActionListener>

<t:updateActionListener value="#{InstanceList.versionFilter}" property="#{InstanceForm.versionFilter}"></t:updateActionListener>

</t:commandButton>

</h:form>

</t:div>

<t:div styleClass="listNavigation">

<f:verbatim>

<br />

</f:verbatim>

</t:div>

<h:form target="form" id="list">

<t:dataTable value="#{InstanceList.instances}" var="rs" sortable="true" rowClasses="oddRow, evenRow" styleClass="list" align="center" cellpadding="3" cellspacing="0" width="100%" rowOnClick="document.getElementById('list').target='form';this.childNodes[0].childNodes[0].click();">

<t:column>

<f:facet name="header">

<h:outputText value="Changed" />

</f:facet>

<t:commandButton value="#{rs.instance}" action="gotoInstanceUpdateForm" style="display: none;">

<t:updateActionListener property="#{InstanceForm.domicileFilter}" value="#{InstanceList.domicileFilter}" />

<t:updateActionListener property="#{InstanceForm.versionFilter}" value="#{InstanceList.versionFilter}" />

<t:updateActionListener property="#{InstanceForm.instanceFilter}" value="#{rs.instance}" />

<t:updateActionListener property="#{UniqueTaskMasterListBox.domicileFilter}" value="#{InstanceList.domicileFilter}" />

<t:updateActionListener property="#{UniqueTaskMasterListBox.versionFilter}" value="#{InstanceList.versionFilter}" />

<t:updateActionListener property="#{UniqueTaskMasterListBox.instancefilter}" value="#{rs.instance}" />

</t:commandButton>

<h:outputText value="#{rs.mut}" />

</t:column>

<t:column>

<f:facet name="header">

<h:outputText value="Key" />

</f:facet>

<h:outputText value="#{rs.key}" />

</t:column>

<t:column>

<f:facet name="header">

<h:outputText value="Instance" />

</f:facet>

<h:outputText value="#{rs.instance}" />

</t:column>

<t:column>

<f:facet name="header">

<h:outputText value="Task Master" />

</f:facet>

<h:outputText value="#{rs.taskMaster}" />

</t:column>

<t:column>

<f:facet name="header">

<h:outputText value="Type" />

</f:facet>

<h:outputText value="#{rs.type}" />

</t:column>

<t:column>

<f:facet name="header">

<h:outputText value="Comment" />

</f:facet>

<h:outputText value="#{rs.comment}" />

</t:column>

</t:dataTable>

</h:form>

</f:view>

</body>

</html>

Here ist the Managed Bean:

package instance;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

publicclass InstanceList{

public Instance[] instances;

public String domicileFilter;

public String versionFilter;

public InstanceList(){

}

public Instance[] getInstances()throws IllegalArgumentException, SQLException{

System.out.println("instance");

Statement stmt = sql.connection.getConnection().createStatement();

ResultSet rs = stmt.executeQuery("call getDVInstances('" + domicileFilter +"','" + versionFilter +"', null)");

ArrayList<Instance> allInstances =new ArrayList<Instance>();

while (rs.next()){

int mut = rs.getInt("cMut");

int key = rs.getInt("cKey");

String instance = rs.getString("kInstance");

int taskMaster = rs.getInt("rTaskMaster");

String type = rs.getString("dInstanceType");

String comment = rs.getString("dComment");

allInstances.add(new Instance(mut, key, instance, taskMaster, type, comment));

}

Instance[] instances =new Instance[allInstances.size()];

for (int i = 0; i < allInstances.size(); i++){

instances[i] = allInstances.get(i);

}

stmt.close();

return instances;

}

public String getDomicileFilter(){

return domicileFilter;

}

publicvoid setDomicileFilter(String domicileFilter){

this.domicileFilter = domicileFilter;

}

public String getVersionFilter(){

return versionFilter;

}

publicvoid setVersionFilter(String versionFilter)throws SQLException{

this.versionFilter = versionFilter;

}

}

Thank you in advance for any help

Patrick[/nobr]

[10935 byte] By [acrobata] at [2007-11-26 17:22:21]
# 1

You're using a request scoped bean which is reinitialized on every request, isn't it?

Change it to session scope or just split the data logic into a session scoped bean.

Also see the CRUD example in the EAR of http://balusc.xs4all.nl/srv/dev-jep-dat.html for some practices.

And this article might be interesting to understand the JSF lifecycle: http://balusc.xs4all.nl/srv/dev-jep-djl.html

BalusCa at 2007-7-8 23:50:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...