Managed bean and view scope and Partial update

UI Components for JSF
Post Reply
vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

02 Dec 2011, 23:48

Hi ,

i don't know if this should be raised here or not . Thanks in advance if someone can solve this

I have a view scoped managed bean . In the constructor of the managed bean i have a code which populates the datatable on the page load . All works well .
Now i have a command link on the data table cell and other command buttons on the page . Now whenever i do any action on the page the constructor of the bean is invoked again even though no navigation or any action to navigate has been performed . Just to add to it the command button action or command action listener is not bind to same bean .

sample code is :
<p:dataTable var="current" id="tbl" widgetVar="current" filterEvent="enter"
rowStyleClass="datatablerow" value="#{accounts.accountsList}"
paginator="true" rows="10" styleClass="mystyle" sortBy ="#{current.invName}"
paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<f:facet name="header">
<p:outputPanel style="float:right">
<h:outputText value="Search all fields:" style="color:white;" />
<h:inputText id="globalFilter" onkeyup="current.filter()" />
</p:outputPanel>
</f:facet>

<p:column style="width:10px">
<p:rowToggler />
</p:column>

<p:column headerText="Investor Name" sortBy="#{current.Name}"
filterBy="#{current.Name}" filterMatchMode="contains">
<p:commandLink id="NameLink" value="#{current.ame}"
style="text-align:left;" update="form:display"
actionListener="#{ivrdtl.cellClickedIvrName(current.accntNumber)}"
oncomplete="InveDetailsDialog.show()" />
</p:column>

<p:column sortBy="#{current.accntNumber}" style = "text-align:center;"
filterBy="#{current.accntNumber}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Account Number" />
</f:facet>
<p:commandLink value="#{current.accntNumber}"

actionListener="#{details.Details(current)}" update = "form:Detail">
</p:commandLink>
</p:column>

<p:panel collapsed="true" id="Detail" header="Account Details"
toggleable="true" styleClass="myPanelStyle myPanelStyleTitle"
toggleSpeed="500" widgetVar="panelDetails">
</p:panel>

<p:dialog header=" Details " id = "display"
resizable="true" dynamic="true" modal="true" showEffect="boounce"
hideEffect="fade" width="670" widgetVar = "DetailsDialog" position="300,200">
<ui:include src="PROFILE.xhtml"></ui:include>
</p:dialog>

Everything is inside the same form component . Please suggest if this is the correct behavior as this results in constructor being called everytime and a call to populate the datatable for every action even though it is supposed to update only a part of page .

Using PF 3.0 RC1 , Mojarra 2.1.3 , WAS 6.1

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

03 Dec 2011, 02:44

i was trying to get some idea on how it works . came across this :
http://stackoverflow.com/questions/5541 ... ax-request

is it a bug or is it suppose to behave this way ?

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

03 Dec 2011, 17:24

It seems a bug with JSF view mode and resolution is targeted for JSF 2.2 . Is there any way to get around this?

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

03 Dec 2011, 21:06

You should use @PostContruct an not the constructor
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

03 Dec 2011, 22:18

I tried that but that also is being called multiple times

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

03 Dec 2011, 22:43

You could set something like an initialize flag.

Code: Select all


private boolean initialized = false;

@PostConstruct
public void myMethod() {
  if (!intialized) {
    initialized = true;
   
    Your code......
  }
}

Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

05 Dec 2011, 05:26

The above solution solves the problem partially . Actually the problem is the ui :include on the page .

sample code is :
<p:dataTable var="current" id="tbl" widgetVar="current" filterEvent="enter"
rowStyleClass="datatablerow" value="#{accounts.accountsList}"
paginator="true" rows="10" styleClass="mystyle" sortBy ="#{current.invName}"
paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<f:facet name="header">
<p:outputPanel style="float:right">
<h:outputText value="Search all fields:" style="color:white;" />
<h:inputText id="globalFilter" onkeyup="current.filter()" />
</p:outputPanel>
</f:facet>

<p:column style="width:10px">
<p:rowToggler />
</p:column>

<p:column headerText="Investor Name" sortBy="#{current.Name}"
filterBy="#{current.Name}" filterMatchMode="contains">
<p:commandLink id="NameLink" value="#{current.ame}"
style="text-align:left;" update="form:display"
actionListener="#{ivrdtl.cellClickedIvrName(current.accntNumber)}"
oncomplete="InveDetailsDialog.show()" />
</p:column>

<p:column sortBy="#{current.accntNumber}" style = "text-align:center;"
filterBy="#{current.accntNumber}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Account Number" />
</f:facet>
<p:commandLink value="#{current.accntNumber}"

actionListener="#{details.Details(current)}" update = "form:Detail">
</p:commandLink>
</p:column>

<p:panel collapsed="true" id="Detail" header="Account Details"
toggleable="true" styleClass="myPanelStyle myPanelStyleTitle"
toggleSpeed="500" widgetVar="panelDetails">
<h:panelGrid columns = "2" columnClasses = "userinfostyle">
<h:outputText value = "Account Type : CASH" style = "font-weight:bold"></h:outputText>
<h:outputText value = "Account Number : 0123"></h:outputText>
</h:panelGrid>
<p:tabView dynamic="true" cache="false" styleClass="mytabs" id = "test1">
<p:tab title="Details" disabled = "#{accounts.detailsRendered}">
<ui:include src = "#{accounts.accDtlType}"></ui:include>
</p:tab>

<p:dialog header=" Details " id = "display"
resizable="true" dynamic="true" modal="true" showEffect="boounce"
hideEffect="fade" width="670" widgetVar = "DetailsDialog" position="300,200">
<ui:include src="PROFILE.xhtml"></ui:include>
</p:dialog>

If i remove the ui:include from the tab it works fine else it calls constructor even for filtering , Sorting or any other action on page . Please suggest what can be the work around for this .

porthos
Posts: 122
Joined: 09 May 2011, 03:17

05 Dec 2011, 11:49

Yes, ui:include will cause ViewScope to behave that way. The problem should go away if you convert your include file to a JSF composite component.
-------
PrimeFaces 3.4, Glassfish 3.1.2, Mojarra 2.1.6

vineet
Posts: 387
Joined: 14 Oct 2011, 23:40

06 Dec 2011, 05:09

That does the trick . Now the problem is i have to include the composite depending on criteria which is to be bind to backing bean . something like

<{"bean.somevariable> where some variable will be a composite value like util:example . i guess it is not possible and only solution is to have boolean to check rendering of composites .

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 59 guests