This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Form calling a session scoped backing bean constructor twice

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
<h:outputStylesheet 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>
<h:outputText 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>
<h:outputText 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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Saloon Keeper
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Christopher Whu
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28325
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dump the stack trace (or put a breakpoint and look at the stack) in the constructor. It might tell you something.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic