ActionListener method not called when in a conditional rendered block - jsf

I've got this code where some buttons show only under a certain condition, like this:
<h:panelGroup rendered="#{mayusculasBean.jefeCerca}">
<h:panelGrid columns="4">
<h:commandButton value="Listar"
actionListener="#{gestorEmpleados.listarEmpleados}"
immediate="true"/>
...`
</h:panelGroup>
The rendered attribute is working ok. The problem is that the actionListener method is not being called when the commandButton is pressed. However, if I get rid of the rendered attribute, the button is working properly.
I think it might be related to the different phases of the Jsf request, so when the commandButton is pressed the rendered attribute, I don't know why, evaluates to false, therefore avoiding the call to the actionListener method... but it's just a guess, I really have no idea.
Any help?

If your mayusculasBean is #RequestScoped, then, after response committed, your command button component will not be tracked by the server.
If this is your case, you need to change your mayusculasBean to a wider scope such as #ViewScoped.

Related

ui:repeat with a list sending right object to a p:dialog

I currently have a giant ui:repeat. Within this ui:repeat, some of the repeated objects have a url to a popup image associated with them. When someone clicks display under that particular object, I need the url to popup in a p:dialog.
<ui:repeat var="thing" value="#{bean.thingList}">
<p:commandLink value="details" onclick="miniImage.show();"
update=":#{p:component('chart')}"
action="#{bean.setCurrentImg(thing.imageUrl)}"
rendered="#{thing.includeImage}">
</p:commandLink>
</ui:repeat>
and at the bottom of the page:
<p:dialog id="chart" widgetVar="miniImage" >
<h:graphicImage value="#{bean.currentImg}"/>
</p:dialog>
And in the backing bean I tried using a simple setter and getter for currentImg.
I am a bit confused on this now and would like to accomplish this without having to submit the entire form as well. Any help is greatly appreciated.
If you're using PrimeFaces 3.3 or newer, you could just add partialSubmit="true" to the command component. You can then control the to-be-processed components in process attribute. In this particular case, just the current component (the command component itself) is sufficient, thus so process="#this":
<p:commandLink ... process="#this" partialSubmit="true" />
This way only the request parameters which are really necessary for the process will be sent.
Unrelated to the concrete problem, I suggest to use oncomplete instead of onclick to open the dialog. Otherwise the dialog is opened before update takes place and may cause poor user experience as the enduser would see the image instantly changing.

PrimeFaces dialog validation errors

What i want to do is like basic row selection example at Primefaces showcase(http://www.primefaces.org/showcase/ui/datatableRowSelectionByColumn.jsf) I want to update my datatable's row. The problem is when i click to update button at datatable, dialogbox appears with validation errors.
Second thing is what is the order of method execution times.(action-update-onclick-f:setPropertyActionListener)
<p:commandButton id="updateButtonId"
action="#{myController.showCompanyEditPanel}"
update=":tabView:companyForm:companyEditPanel"
onclick="companyDialog.show()"
icon="ui-icon-pencil" title="update">
<f:setPropertyActionListener value="#{company}" target="#{myController.selectedCompany}" />
</p:commandButton>
<p:dialog id="editCompanyDialogId" header="CompanyEdit" widgetVar="companyDialog" resizable="false">
<p:panel id="companyEditPanel" >
//some stuff here
</p:panel>
</p:dialog>
You seem to be missing a major point of using a <p:commandButton> here, as well as seem to be mixing client-side and server-side events.
First on <p:commandButton>. This component is designed to POST (partial) form data to the current URL, do business job in action(listener) method and return updated components / perform navigation. You can of course 'attach' JavaScript events to all those attributes.
Second, onclick, oncomplete, and other on... attribute are corresponding to some client-side events. In particular, onclick function is triggered when button was clicked, oncomplete function is called when DOM was updated after the AJAX call, i.e. the elements specified in <p:ajax update="..."> or simply in update="..." attribute of <p:commandButton>.
Third, all action listeners (thus, actionListener attribute, <f:actionListener> tag, <f:setPropertyActionListener> tag) will be executed right in the order they are specified in your tag, see this answer for more elaboration. The last one to be executed is action method, after which response is sent back.

Reaching backingbean method in datatable column JSF/PrimeFaces

I am unable to reach my backing beans method when calling it in a <p:commandLink> inside a datatable column.
My commandlink works fine when put outside the datatable, but then I cannot directly pass the selected row variable.
Here is my code:
<h:form id="reviewLists" prependId="false">
<p:messages />
<p:panel header="Beoordelingen" style="margin-bottom:10px;">
<p:dataTable value="#{reviewFinderBean.employees}" var="employee" >
<p:column headerText="Medewerker" >
<h:commandLink value="#{employee.name}" action="#{reviewFinderBean.showReviewsForEmployee(employee)}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
When checking the http requests my browser makes I see it does another post (ajax) as expected, I have tried to use prependId="false" as I thaught the generated component names might have been unresovable but that didnt help.
The ajax post is fired but somehow is never resolved to the correct backingbean method on the server
<f:setPropertyActionListener> also doesnt resolve to any property when set correctly and used in the the datatable column.
First of all, get rid of prependId="false". It makes things worse in ajax processing and updates.
In order to fix the problem, you need to rewrite the bean in such way that it returns exactly the same data model (the value behind #{reviewFinderBean.employees}") during processing the form submit as it was during displaying the form. JSF will namely re-iterate over it in order to find the associated row where the command is been invoked.
If you want to keep the bean in the request scope, then you need to recreate exactly the same datamodel in its (post)constructor. If your bean is already in the view scope, then you need to make sure that the getter method is totally free of business logic so that the data model don't potentially change.
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 4

Modify dynamically JSF snippets by h:commandlink

I have a h:commandlink control in page1. the control uses f:ajax to call to the following h:panelgroup :
I have a h:panelgroup control in page2 (a snippet), which has a ui:include within it.
I have a h:panelgroup control in page3 (a snippet), which has a ui:include within it.
Now according to the choices made on page1, I would like to switch the snippets by clicking on the h:commandlink control.
I have a BIG problem there: it seems that only if I click twice on the commandlink, only then the snippet changes - and not on one click.
I have tried to remove the f:ajax to render the panelgroup, and still it does not work...
There are two potential causes of this problem.
The <f:ajax> is fully re-rendering another <h:form> than where it is sitting in. This way the view state of the other form will get lost which would require invoking the action on the other form twice before it really get executed.
The solution is to not re-render the other <h:form>, but only some container component in that form. E.g.
<h:form id="otherForm">
<h:panelGroup id="content">
...
<h:panelGroup>
</h:form>
with
<f:ajax render=":otherForm:content" />
When there's a rendered attribute on the <h:commandLink> or any of its parent components, then it must evaluate true during the apply request values phase of the postback request in order to get JSF to invoke the bean action associated with the <h:commandLink> during the invoke action phase of that request. Perhaps the bean is request scoped and/or some odd/illogical flow inside the bean caused that the rendered attribute is not properly been preserved.
Best is to maintain those rendered conditions in a #ViewScoped bean and let its action methods return void or null so that the bean lives as long as you're interacting with the same view. Change the rendered conditions during action methods only and not inside setters/getters or something.

ReRendering a JSF Component

Can I rerender a jsf ui component when a valuechangelistener method is run?
The reason i'm asking is that my valuechangelistener method changes the values of the input boxes in the backing bean but they don't seem to be rerender.
What happens eventually is that the values that are printed on screen are saved to the backing bean, overriding the values loaded through the valuechangelistener method.
The following doesn't work:
<h:inputText id="inputbox_id" value="#{name}"/>
<h:selectOneMenu valueChangeListener="#{myBean.changeCountryMenu}">
<a4j:support event="onchange" rerender="inputbox_id" action="#{bean.test}>
</h:selectOneMenu>
Notice that bean.test() is never run. (UPDATE: It does, I had a renderResponse() that skipped it before. The values still do not reRender though)
So the solution I thought of is to rerender the inputbox from the valueChangeListener.
If there is some other better solution i'd be glad to hear...
Thank you!
Ben.
The attribute name is reRender (with capital R the second time). Perhaps you have to add immediate="true" ?
Also, check this article

Resources