aspose file tools*
The moose likes JSF and the fly likes Form calling a session scoped backing bean constructor twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Form calling a session scoped backing bean constructor twice" Watch "Form calling a session scoped backing bean constructor twice" New topic
Author

Form calling a session scoped backing bean constructor twice

Christopher Whu
Ranch Hand

Joined: Sep 03, 2008
Posts: 80
I have a session scoped backing bean.. when the form loads it is calling the bean twice. Is there anyway to just get one call to the bean? i think the 2 h:selectOneMenu are making the call but i am not sure....

this is the page

<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<hutputStylesheet library="css" name="styles.css"/>
<title>#{msgs.windowTitle}</title>
</h:head>
<h:body>

<h:form>


<h:panelGrid columns="2"
styleClass="mainArea"
columnClasses="mA_left,mA_right" >
<h:column>
category:
</h:column>
<h:column>
<h:selectOneMenu value="#{productTable.chosenCategoryId}" converter="com.converters.ObjectStringCoverter" valueChangeListener="#{productTable.categoryChanged}"
styleClass="dropDown" id="category" onchange="submit()" rendered="#{productTable.categoryEditable}">
<f:selectItems id="categorySelection" value="#{productTable.categoryArrayList}" var="category" itemLabel="#{category.categoryName}" itemValue="#{category.categoryId}" />
</h:selectOneMenu>
<hutputText value="#{productTable.chosenCategory.categoryName}" rendered="#{not productTable.categoryEditable}"/>
</h:column>
</h:panelGrid>


<h:panelGrid columns="2"
styleClass="mainArea"
columnClasses="mA_left,mA_right" >
<h:column>
product line:
</h:column>
<h:column>
<h:selectOneMenu value="#{productTable.chosenProductLineId}" converter="com.converters.ObjectStringCoverter" styleClass="dropDown" id="productLine"
valueChangeListener="#{productTable.productLineChanged}" onchange="submit()" rendered="#{productTable.productLineEditable}">
<f:selectItems id="productLineSelection" value="#{productTable.productLineArrayList}" var="productLine" itemLabel="#{productLine.productLineName}" itemValue="#{productLine.productLineId}" />
</h:selectOneMenu>
<hutputText value="#{productTable.chosenProductLine.productLineName}" rendered="#{not productTable.productLineEditable}"/>
</h:column>

</h:panelGrid>

<h:panelGrid columns="2"
styleClass="mainArea"
columnClasses="mA_left,mA_right" >
<h:column>
reset model
</h:column>
<h:column>
<h:commandButton value="reset" action="#{productTable.resetProducModel}"/>
</h:column>
</h:panelGrid>

<h:dataTable value="#{productTable.productArrayList}" var="productArrayList"
styleClass="productTable"
headerClass="columnHeader"
footerClass="columnHeader"
rowClasses="oddColumn,evenColumn">
<f:facet name="caption">Drop Down</f:facet>
<f:facet name="caption">All Products</f:facet>
<h:column>
<f:facet name="header">#{msgs.col_prodName_header}</f:facet>
#{productArrayList.name}
</h:column>
<h:column>
<f:facet name="header">#{msgs.col_prodCategory_header}</f:facet>
#{productArrayList.category}
</h:column>
</h:dataTable>
</h:form>
<h:messages />
</h:body>
</html>



the class is huge but the beginning is
Christopher Whu
Ranch Hand

Joined: Sep 03, 2008
Posts: 80
i think acutally setting @named in the backing bean and referencing that fixed it... I think the book i am using never set it because of multiple examples having the same class name...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Christopher, you can use the "code" tags for XML and stuff like that as well as for java code. It makes them a LOT easier to read!

A constructor is called exactly once and ONLY once for an instance of a JavaBean. That's a core specification of the Java language.

However get/set methods can be invoked multiple times on a JSF View request. You should expect that - it's normal behaviour. Which is why "get" and "set" methods should not have side-effects.


Customer surveys are for companies who didn't pay proper attention to begin with.
Christopher Whu
Ranch Hand

Joined: Sep 03, 2008
Posts: 80
it still happens once and a while but only the first time view the page after a server reload so its probably a Glssfish thing... It will never happen after i use the page once... Very weird...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

I wouldn't count on it being a Glassfish thing. But occasionally people do manage to get more than one instance of a bean created in a JSF app. It's happened to me once or twice but it always turned out to be a bug in my app, not the server.

Where it gets bad is when one instance gets some of the data and the other instance gets the rest of the data. You end up with 2 garbled beans that break in strange ways.
Christopher Whu
Ranch Hand

Joined: Sep 03, 2008
Posts: 80
When it goes bad it doubles my DropDown menu contents... I have since written some code to look out for this but i still see the constructor going off twice...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Dump the stack trace (or put a breakpoint and look at the stack) in the constructor. It might tell you something.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Form calling a session scoped backing bean constructor twice