in a brief word here is the problem
when i value change of the menu it execute the post-constructor again in backing bean then execute method fillAreas and i used view scope .
<p:selectOneMenu id="governate" value="#{add.selectedGovern}" style="height:26px; text-align: right; width: 303px;"
valueChangeListener="#{add.fillAreas}" rendered="#{languageBean.isDefaultLanguage==true}" immediate="true" >
<f:selectItem itemLabel="---#{prompts._select} ---" itemValue="0"/>
<f:selectItems value="#{add.governrateList}" var="govern" itemLabel="#{govern.governrateName}" itemValue="#{govern.governrateId}" />
<f:ajax immediate="true"/>
</p:selectOneMenu>
what can i do to prevent calling post-constructor ? and if post-constructor called once why it called every time i select from menu ?
Thanks in advance .
A view scoped bean will be recreated on every single request when a property of it is been bound to an attribute of a taghandler (JSTL <c:forEach>, Facelets <ui:include>, JSF <f:validateXXX>, etc) or to the binding or id attribute of a JSF UI component (<h:someComponent>, etc).
This is related to JSF issue 1492. You could solve it by disabling the partial state saving for the specific view, or by looking for alternate ways to achieve the desired functional requirement.
See also:
Communication in JSF 2.0 - #ViewScoped fails in taghandlers
Related
How can I use (eg ajax) to show a specific jsf element if I click the button where the ajax tag belongs to? But without having to use any backing bean, just by JSF components without JavaScript?
Is that possible?
Luiggi is right , You better use js for client side toggling
But ,
I think you can bind like this
<h:inputText id="first" binding="#{someViewButNonManagedBeanObject}"`...
and than
<h:inputText id="second" render="#{no empty someViewButNonManagedBeanObject}"`...
or
<h:inputText id="second" render="#{someViewButNonManagedBeanObject.value eq 'wow'}"`...
(Not tested)
I have a <p:dataTable> in a facelet. The facelet is included into a page. A selection of a table row should trigger an update on an element of the surrounding page.
When I include the <p:ajax event="rowSelect" update=":open:separate" /> inside the table, all works fine. But I am using the table on multiple pages and cannot guarantee a constant Id for the updated element.
I have tried removing the <p:ajax> from the table and wrap the included table inside it like this:
<p:ajax event="rowSelect" update=":open:separate">
<ui:include src="/open/components/submittersTable.xhtml"/>
</p:ajax>
I get this error (root cause abridged):
javax.servlet.ServletException: /open/index.xhtml #30,59 <p:ajax> Event:rowSelect is not supported.
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)
root cause
javax.faces.view.facelets.TagException: /open/index.xhtml #30,59 <p:ajax> Event:rowSelect is not supported.
org.primefaces.component.behavior.ajax.AjaxBehaviorHandler.applyAttachedObject(AjaxBehaviorHandler.java:148)
org.primefaces.component.behavior.ajax.AjaxBehaviorHandler.apply(AjaxBehaviorHandler.java:126)
Am I having wrong ideas about what is possible with <p:ajax>? Do I have to include it into the table and try forcing the ids on the surrounding pages to fit? Or is it possible to wrap include with ajax and I'm just doing it wrong?
Thank you
No you can't because of event="rowSelect". Some explanation of event="rowSelect" mean 'a row in the datatable is selected'. So you cannot use this outside of p:dataTable.
I have found a different approach for this - setting ui parameters for the facelet is definitely better than enforcing Ids for all views using the facelet.
One can safely leave the <p:ajax> tag inside the facelet table, just define a param on the inclusion site:
<ui:include src="/open/components/submittersTable.xhtml">
<ui:param name="updateParam" value=":open:content" />
</ui:include>
and change the updated attribute to use this param:
<p:ajax event="rowSelect" update="#{updateParam}" />
I use Primefaces 3.2 with ApacheMyfaces on WebSphere Application Server 8.
I have a selectOneButton with an ajax update inside.
When I switch the button, my setter firsts sets the value (int) to 0 and then to the value which is selected:
<p:selectOneButton value="#{graph.view}" id="view">
<f:selectItem itemLabel="W" itemValue="1" />
<f:selectItem itemLabel="M" itemValue="2" />
<f:selectItem itemLabel="Y" itemValue="3" />
<p:ajax event="change" update=":optform:datecol"/>
</p:selectOneButton>
datecol is another selectComponent inside my form (optform).
Why does JSF first set the value to 0 and then to e.g. 2?
SOLUTION
It is a PrimeFaces selectOneButton Bug. See My question here .
Best Regards
Veote
Likely what you are seeing is that the managed bean graph is being instantiated early in the JSF lifecycle, and only later in the APPLY_REQUEST_VALUES phase is the appropriate value being set for the managed property view. The managed bean regardless of scope, will be reinstantiated after each request due to the inherently stateless nature of the web.
For more information, BalusC has a great article on debugging the JSF Lifecycle with a step-by-step walkthrough on how to implement a custom PhaseListener that will help you understand and debug behavior like this in your application.
http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html
I'm working on an application using JSF 2.0 and Richfaces 4, that consists of many tables that display elements and of course, the usual View/Edit/Delete options. After some SO browsing and Google search I've decided to post a question because the answers I found did not solve my problem.
Right now, and going straight to the point, my application is having issues when handling certain attributes that are stored in request beans and, on certain points, are lost due to successive requests.
For example, when I want to edit an object, the object is sent (f:propertyActionListener) to a request bean that displays the data on a form, then it is discarded as that request ends. When saving, a new object is created and the attributes on the form are setted to it and the item gets saved instead of updated, since it has no id (JPA + Hibernate).
I've investigated many options and this is what I've did so far and the results:
f:param + h:link or h:commandLink: With #ManagedProperty the param is null, and I can't find it on the Context to look it up through JNDI.
f:setPropertyActionListener + h:commandLink + Request Bean: Works... but I'm losing some data. The form that displays the data has some conditionally rendered fields and I can't hold that info, so the form is messed if the validation phase finds invalid data.
f:viewParam + h:commandLink + View Scoped Bean: Weird stuff here. This one doesn't directly work because the bean seems to get discarded before rendering the form, because the form is rendered with no information since the bean is clean.
Using a session bean: Works like a charm, but I don't want to make a session bean for every form just because I'm still learning things about the JSF lifecycle, I want to do it the proper way.
If I want to keep the Request session approach, is there a way to store a parameter (either an object or a plain string) and obtain later on a request bean?.
Dunno if this helps but I'm using a master page through ui:insert and ui:define.
Use a view scoped bean. It should work. The problems which you describe there suggests that you're binding it to JSTL tags or id or binding attributes. You should not do that on a view scoped bean. See also #ViewScoped fails in tag handlers. Another possible cause is that you're using CDI's #Named to manage the bean instead of JSF's #ManagedBean. That would also explain why #ManagedProperty doesn't work in one of your attempts as it also requires the bean to be managed by JSF's #ManagedBean.
As to the master-detail page approach, use a <h:link> with <f:param> in the table page to create view/edit links in the master page.
E.g. user/list.xhtml
<h:dataTable value="#{userList.users}" var="user">
<h:column>#{user.id}</h:column>
<h:column>#{user.name}</h:column>
<h:column>
<h:link value="Edit" outcome="edit">
<f:param name="id" value="#{user.id}" />
</h:link>
</h:column>
</h:dataTable>
The bean can be just request scoped.
Then, in the defail page, which is in this case an edit page, use <f:viewParam> to convert, validate and set the id as User.
E.g. user/edit.xhtml
<f:metadata>
<f:viewParam name="id" value="#{userEdit.user}"
converter="#{userConverter}" converterMessage="Bad request. Unknown user."
required="true" requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>
<h:messages />
<h:link value="Back to all users" outcome="users" />
<h:form id="user" rendered="#{not empty userEdit.user}">
<h:inputText value="#{userEdit.user.name}" required="true" />
...
<h:commandButton value="Save" action="#{userEdit.save}">
<f:ajax execute="#form" render="#form" />
</h:commandButton>
</h:form>
Use a #ViewScoped bean to hold the data, service and action methods:
#ManagedBean
#ViewScoped
public class UserEdit {
private User user;
#EJB
private UserService service;
public String save() {
service.save(user);
return "users";
}
// Getter+setter.
}
JFS1.2 + Richfaces 3.3
Situation is as follows:
JSP page renders conditionally one or another panelGroup.
Within each panelGroup there are couple setters and one command button.
Each of two panelGroups uses own bean for setting and performing action.
On the top of a page there's selectOneRadio with (obvious) two items - coresponding tow options of conditional rendering.
Page renders properly, switcher causes to render appropriate panel.
Case is, commands buttons doesn't call an action.
I know what's going on - when I click a button to call action dom is regenerated, but the value that hold my decision to display particular panel doesn't exist anymore. The button is not recreated, action is not fired.
Technically:
<h:selectOneRadio value="#{reportType}">
<f:selectItem itemLabel="x" itemValue="x"/>
<f:selectItem itemLabel="y" itemValue="y"/>
<a4j:support event="onclick" reRender="xPanel, yPanel/>
</h:selectOneRadio>
<h:panelGrid id="xPanel "columns="2" rendered="#{reportType eq 'x'}">
<...some setters>
<... commandbutton>
</h:panelGrid>
<h:panelGrid id="yPanel "columns="2" rendered="#{reportType eq 'y'}">
<...some setters>
<... commandbutton>
</h:panelGrid>
Question is, how to design the page to obtain proper rendering and actions?
For now, I created additional session bean that holds switching value (x|y), but that desing smells bad for me...
RichFaces 3.3 offers the <a4j:keepAlive> tag which does basically the same as Tomahawk's <t:saveState> and JSF2 #ViewScoped. Add the following line somewhere in your view:
<a4j:keepAlive beanName="#{bean}" />
This will keep the bean alive as long as you're returning null or void from action(listener) methods.
See also:
JSF 1.2: How to keep request scoped managed bean alive across postbacks on same view?