Why JSF postAddToView event listener method is called during postback calls? - jsf

I added postAddtoView event listener to below component. I was expecting the
orderView.populateForm listener method is being called once during creation of the initial view (https://javaserverfaces.java.net/nonav/docs/2.2/javadocs/javax/faces/event/PostAddToViewEvent.html
).
For any postback call where view is restored I do not expect a new component instance is added to view. Hence no event to fire.
However, event listener method is being called for each postback call.
<h:form id="orderForm">
<p:panelGrid id="attributePanel" columns="#{orderView.numberOfGridColumns}">
<f:event type="postAddToView" listener="#{orderView.populateForm}" />
</p:panelGrid>
<p:commandButton ajax="true" value="Create" process="#form" update="#form"/>
</h:form>
OrderView is a view scoped CDI bean. As far as I understand view scoped beans are stored in the partial view state and by default partial view state saving is enabled. (BalusC gives a nice explanation here about the issue Should PARTIAL_STATE_SAVING be set to false?) To see it has an effect, I disabled/enabled partial state saving. It did not help.
Thanks in advance.
PS: I am running on Mojarra 2.2.12/ Glassfish 4.1

Related

View scoped managed bean's #PostConstruct reinvoked after closing p:dialog

I have a popup defined within my XHTML which gets shown conditionally depending on what selections the user makes in the main screen rendered by default:
<p:dialog id="commentDialogID" header="Enter comment" widgetVar="commentDialog" modal="true" resizable="true" height="auto">
<h:form id="commentForm">
<h:outputLabel for="comment" value="Comment:"/>
<p:inputTextarea id="comment" title="Comment"
rows="6" cols="33"
value="#{managedBean.activeItem.comment}"
required="true">
<f:ajax render="comment"/>
</p:inputTextarea>
<h:commandButton id="commentSubmit" value="Submit" action="#{managedBean.proceed}" onclick="PF('commentDialog').hide();">
<f:ajax render="commentSubmit"/>
</h:commandButton>
</h:form>
</p:dialog>
The problem is that, once this dialog/popup is closed, the container (JBoss) or the framework (JSF/Primefaces), not sure which, thinks that the whole view has been closed and therefore on the next request that triggers an appearance of this popup, it re-invokes the backing bean's #PostConstruct method. The backing bean is #ViewScoped. I really don't want it to do that, instead, I want it to treat the dialog/popup as a div in the page whose closure does not affect the view state.
The first time the dialog is brought up, the #PostConstruct is not invoked as the initial view from rendering the page, which called the #PostConstruct, is still active. However, on the second appearance, it is reinvoked, which leads me to believe it is because it was closed after the first time, which either the container of the framework or both mistake as needing to reload the bean.
What can I do to prevent the backing bean from going into the #PostConstruct after this dialog has been closed?
I know what the problem is..
You are using h:commandButton to submit the form and to close the dialog.
Lets look at your code:
<h:commandButton id="commentSubmit" value="Submit" action="#{managedBean.proceed}" onclick="PF('commentDialog').hide();">
<f:ajax render="commentSubmit"/>
</h:commandButton>
In the above code As soon as you clikc Submit button:
1. Your action will get triggred to call ManagedBean method managedBean.proceed.
2. since you have bound onclick JS event, your dialog gets closed.
After your action="#{managedBean.proceed} comes back it has to update the button with id commentSubmit since you have used render="commentSubmit".
But by the time your action="#{managedBean.proceed} comes back to render="commentSubmit" the disloag in which your button commentSubmit is placed is closed. so this might the reason for re initializing the ManagedBean.
To Avoid this you ca use Primefaces p:commandButton which has oncomplete attribute which is helpfull in this scenario.
<p:commandButton id="commentSubmit" value="Submit" action="#{managedBean.proceed}" update="commentSubmit" oncomplete="PF('commentDialog').hide();" />
So in the above case p:dialog will close after the action is completed.

p:remoteCommand won't fire bean method

I have simple primefaces remoteCommand in composite component that won't trigger method in back bean.
<h:form id="frm1" prependId="false">
<p:remoteCommand name="rc1"
actionListener="#{Bean1.preloadMenu()}"
process="#this"
onstart="alert('xxx')"
autoRun="true"
/>
...
RemoteCommand works, it runs onstart client-side script but it doesn't trigger method in bean (never steps into method, as if it doesn't exist). I get no error on client or server side.
I'll close this topic. I resolved this issue by putting code in initialize (#PostConstruct) methode. Didn't want to do it like that but this dialog isn't used too often and bean is associated just with this component so it shouldn't do much damage.

<h:commandLink> not working inside a <h:dataTable>

I will try to explain myself:
I'm working on a JSF project using java, JSF 2.0 and RichFaces 4.2.1.
When I access my jsf it just loads a search filter and a commandLink. The commandLink will launch a method in my backingBean to load data that it will be displayed in a dataTable.
<h:commandLink id="btnRecords">
<f:ajax render="myCompAjax" event="click" listener="#{myBean.loadRecords}" />
<h:graphicImage value="img/ico_new.gif" alt="#{bundle['button.search']}" />
</h:commandLink>
The datatable is not visible at first, but once you click on the commandLink a flag in the backingBean will change and the table displays with data I just loaded.
<a4j:outputPanel ajaxRendered="true" id="myCompAjax">
<h:dataTable id="recordsTable" value="#{myBean.records}"
var="item" rendered="#{myBean.flagShowTable}">
<h:column headerClass="thPijama" >
<f:facet name="header">
<table><tr class="thPijama"><td></td></tr></table>
</f:facet>
<h:commandLink action="#{myBean.goNextPage}">
<h:outputText value="Go Next Page" />
<h:inputHidden value="#{item}" />
</h:commandLink>
</h:column>
</h:dataTable>
</a4j:outputPanel>
Problem is the commandLink action inside of the dataTable isn't working at all. I just want to navigate to another jsf. In fact, what it does is hiding the dataTable and leaving the filter unchanged. The action method remains unreachable.
Of course, it works if I set the same commandLink outside the dataTable.
I cannot use Session Scope Beans because the people I work for don't approve it.
Can anyone help me?
Thanks for the hint.
I cannot use Session Scope Beans because the people I work for don't approve it.
Are you implying that placing the bean in the session scope instead of the request scope actually solved the problem? If so, then just put the bean in the view scope.
#ManagedBean
#ViewScoped
public class MyBean implements Serializable {
// ...
}
This way the bean will live as long as you're interacting with the same view by ajax requests. The bean is not been shared in other browser tabs/windows in the same session (which is among the architects indeed the major reason to forbid its use in case of simple views).
See also:
How to choose the right bean scope?
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 4 applies to you

JSF ViewScope and Bean creation

I have a problem that i don't understand:
I request a new site. A site has a link that opens a dialog. The link is inside a form.
The dialog is not inside the form.
A reduced code example:
<p:outputPanel id="layout-center" >
<h:form>
<p:commandLink id="option_field_user_profile" actionListener="#{controllerBean.getBean('userProfileBean', component).init}" oncomplete="#{controllerBean.getBean('userProfileBean', component).show}" >
<h:outputText value="#{msg.mProfile}"/>
</p:commandLink>
</h:form>
</p:outputPanel>
<p:dialog header="#{userPreferencesBean.header}" widgetVar="#{userPreferencesBean.widgetVar}" appendToBody="#{userPreferencesBean.appendToBody}" resizable="#{userPreferencesBean.resizable}" id="#{userPreferencesBean.xhtmlId}" dynamic="#{userPreferencesBean.dynamic}" modal="#{userPreferencesBean.modal}" closable="#{userPreferenceBean.closable}">
<ui:include src="/WEB-INF/templates/modification/userPreferences.xhtml" />
</p:dialog>
UserPreferencesBean is in ViewScope. My problem is now that the #PostConstruct method from the UserPreferencesBean is called twice with the non-postback request i.e. the Bean is constructed twice although it should be the same view. If i move the dialog inside the form for testing purposes it is called once, like expected. But since the dialog has its own form this is not a solution, for sure.
When the site is loaded and I hit F5, the PostConstruct method is executed once.
Has somebody an idea?
This is caused because you referenced a view scoped bean property in the view build time attribute id of the <p:dialog>. If you fix the id to be static, or to reference a request or application scoped bean property instead, then your view scoped bean will behave as expected.
See also:
JSTL in JSF2 Facelets... makes sense? - for some background explanation on view build time and view render time; the id and binding attributes of UI components are evaluated during view build time.

RequestScoped bean destroyed in confirmDialog

I have RequestScoped bean because I'm redirecting the user from dataTable with records to edit page. In this dataTable I have delete buttons:
<p:column>
<p:commandButton update=":deleteNewsDialog" oncomplete="deleteNewsDlg.show()" icon="ui-icon-closethick">
<f:setPropertyActionListener value="#{news}" target="#{newsBean.news}" />
</p:commandButton>
</p:column>
And here is the dialog:
<p:confirmDialog id="deleteNewsDialog" message="Czy na pewno chcesz usunąć wiadomość o tytule "#{newsBean.news.title}"?"
header="Usuwanie wiadomości" severity="alert"
widgetVar="deleteNewsDlg" appendToBody="true">
<h:form>
<p:commandButton value="Usuń" actionListener="#{newsBean.delete}" update=":newsesTableForm:newsesTable, :newsesTableForm:newsGrowl"/>
<p:commandButton value="Anuluj" oncomplete="deleteNewsDlg.hide();"/>
</h:form>
</p:confirmDialog>
When newsBean.delete is fired, newsBean not longer exists so I get a lot of ugly validation exceptions. I have other pages like this with ViewScoped beans and there it is working like a charm. Help?
A request scoped bean has a lifetime of exactly one HTTP request/response. So retrieving the entire view with the form is already one HTTP request/response. The request scoped bean is trashed by end of the response. When you fire an ajax request on the view, then you're essentially sending a new HTTP request. This will thus create a new request scoped bean which get trashed by end of HTTP response. So every ajax request on the same view get its own request scoped bean instance.
This is not what you want if you need to maintain data related to the view. You need to put the bean in the view scope instead. The bean will live as long as you're interacting with the same view by ajax and return null or void in action listener methods. Note that when you return a String, even if empty, the view will be recreated and thus the view scoped bean will be trashed.
See also:
How to choose the right bean scope?

Resources