<a4j:commandButton> doesn´t start the action - ajax

I need to use the a4j:commandButton instead of h:commandButton because of its reRender option.
When I'm using h:commandButton, it works fine (but off course without reRender):
<h:commandButton id="save" action="#{bean.save}" value="#{conf.buttonSave}"/>
And the same thing using doesn't (action isn't started even withour reRender option):
<a4j:commandButton id="save" action="#{bean.save}" value="#{conf.buttonSave}"/>
I've also tried:
<h:commandButton id="save" action="#{bean.save}" value="#{conf.buttonSave}">
<a4j:ajax event="click" reRender="table" />
</h:commandButton>
But if I add a4j:ajax it the same problem as a4j:commandButton, action is not started.
Could you help me?

You tagged this JSF 2.0. So you're using JSF 2.0. Just use the JSF 2.0 builtin <f:ajax> tag.
<h:commandButton id="save" action="#{bean.save}" value="#{conf.buttonSave}">
<f:ajax render="table" />
</h:commandButton>
Note that <h:dataTable id="table"> must be within the same <h:form> the above way. Otherwise you need render=":table" instead. Also note that you must have a <h:head> instead of <head> in the template in order to get all necessary JavaScripts auto-included.

Related

oncomplete of p:commandButton not firing within ui:composition

The following code appears in a ui:composition page and uses a template, the template inserts this page using ui:insert.
<p:commandButton id="basic" value="Contact #{viewProfileView.profile.credentials.username}" onclick="PF('dlg1').show();" type="button" />
<h:form id="createMail">
<p:dialog id="createMailDialog" header="Contact #{viewProfileView.profile.credentials.username}" widgetVar="dlg1" modal="true" draggable="false">
<p:growl id="growl"/>
<p:inputTextarea value="#{viewProfileView.emailBody}"
rows="5" cols="80" maxlength="399"
counter="charsLeft"
counterTemplate=" {0} chars remaining."
autoResize="false" />
<h:outputText id="charsLeft" />
<p:commandButton id="sendMailButton" value="Send"
action="#{viewProfileView.sendMail}"
update="growl"
oncomplete="PF('dlg1').hide();"/>
</p:dialog>
</h:form>
I'm using latest JSF and Primefaces 5.1.
Everything works fine except for the oncomplete of the 'sendMailButton' commandButton fails to be called to close the dialog. I know this code works as I have put it in a page of its own and seen it working, it just doesnt want to work as part of a ui:composition with template.
Help would be most appreciated as i've been searching for an answer without luck.
UPDATE: If I change 'oncomplete' to 'onsuccess' it works, but only once, however this does not resolve my issue.
Many thanks.
It appears that it was the template that was causing problems. Working now.

Image not updated after clicking the ajax enabled command link

I am wrirting a captcha code for my jsf project. I have done almost everything but the problem is the refreshing og the image. I'd like to do it with ajax so that when I click the image, it be replaced by new one. But when I click it the image doesn't get updated. But it is updated after reloading the page.
<h:form>
<h:commandLink>
<h:graphicImage value="/captcha/test.png"
style="width:35mm;height:2cm;" />
<f:ajax render="#all" listener="#{captcha.recaptcha()}"></f:ajax>
</h:commandLink>
</h:form>
every time I click the image , it is changed on disk but not updated in the html page.
Thanx for any useful responses !
Try the following code using execute and render tags
<h:form>
<h:commandLink>
<h:graphicImage value="/captcha/test.png" style="width:35mm;height:2cm;" />
<f:ajax render="#form" execute="#form" listener="#{captcha.recaptcha()}" />
</h:commandLink>
</h:form>
You have need to use update attribute.
take a look-
<h:form id="form1">
<h:commandLink>
<h:graphicImage value="/captcha/test.png"
style="width:35mm;height:2cm;" id="img1"/>
<f:ajax render="#all" listener="#{captcha.recaptcha()}" update=":form1:img1"></f:ajax>
</h:commandLink>
</h:form>
Hope this will work

JavaServerFaces 2, returning html by ajax

<h:inputText value="#{finder.valor}" title="Test" id="valor"/>
<h:commandButton value="Search">
<f:ajax execute="valor" render="output"/>
</h:commandButton>
<br/>
<h:outputText id="output" value="#{finder.find}"/>
I wanna do an ajax interactive finder, this finder returns a list of elements (<li>Element1</li><li>Element2</li>...)
There is any way to read that htmlTags in the outputText? Because now it appears like plain text.
Thank you.
I'm not sure whether your question is the following:
How to output HTML-tags with JSF?
Assuming it is, try this:
<h:outputText id="output" value="#{finder.find}" escape="false" />
By default JSF will escape all codes. However, you can disable it with escape="false".

Ajax not working in JSF

I don't know why Ajax is not working in my JSF files.Even I have a latest Mojarra which is 3.2.13 in my GlassFish. Here is my code
<h:head></h:head>
<h:body>
<h:panelGroup id="panel" rendered="#{chartBean.showCharts}">
<p:barChart id="basic" value="#{chartBean.categoryModel}"
legendPosition="ne" title="Basic Bar Chart" min="0" max="200"
style="height:300px" />
</h:panelGroup>
<h:commandButton value="Display Chart" action="#{chartBean.createCategoryModel}">
<f:ajax render="panel"/>
</h:commandButton>
</h:body>
Now when I run this code, button doesn't work.
Another thing, if I change my code like remove panel and just render on barchart like
<f:ajax render="basic"/> and placed it in <form>
then it throws NullPointerException. Because it render the chart and expect value from chartBean.categoryModel. Now AFAIK it should not be rendered because of ajax.
I am unable to understand ajax behavior in JSF. I also tried <f:ajax execute="#form" render="basic"> but it didn't work too. How is this caused and how can I solve it?
You cannot re-render a component that is not there on your page at the time you click the ajax button.
A simple solution would be to move the rendered attribute from the h:panelGroup to the p:barChart. Then the panelGroup will always be rendered and JSF finds it during your ajax request.
<h:panelGroup id="panel">
<p:barChart rendered="#{chartBean.showCharts}"
id="basic" value="#{chartBean.categoryModel}"
legendPosition="ne" title="Basic Bar Chart" min="0" max="200"
style="height:300px" />
</h:panelGroup>
<h:commandButton value="Display Chart" action="#{chartBean.createCategoryModel}">
<f:ajax render="panel"/>
</h:commandButton>

JSF form post with AJAX

I want the following form to use AJAX. So the comments are shown after clicking the command button and without reloading the page. What needs to be changed, using Java Server Faces 2.0?
Functionality: This form provides an inputText to define a topic. After pressing the commandButton, it is searched for comments regarding this topic. Comments are shown in a dataTable, if there are any. Otherwise Empty is shown.
<h:form id="myForm">
<h:outputLabel value="Topic:" for="topic" />
<h:inputText id="topic" value="#{commentManager.topic}" />
<h:commandButton value="read" action="#{commentManager.findByTopic}" />
<h:panelGroup rendered="#{empty commentManager.comments}">
<h:outputText value="Empty" />
</h:panelGroup>
<h:dataTable
id="comments"
value="#{commentManager.comments}"
var="comment"
rendered="#{not empty commentManager.comments}"
>
<h:column>
<h:outputText value="#{comment.content}"/>
</h:column>
</h:dataTable>
</h:form>
You need to tell the command button to use Ajax instead. It's as simple as nesting a <f:ajax> tag inside it. You need to instruct it to submit the whole form by execute="#form" and to render the element with ID comments by render="comments".
<h:commandButton value="read" action="#{commentManager.findByTopic}">
<f:ajax execute="#form" render="comments" />
</h:commandButton>
Don't forget to ensure that you've a <h:head> instead of a <head> in the master template so that the necessary JSF ajax JavaScripts will be auto-included.
<h:head>
...
</h:head>
Also, the element with ID comments needs to be already rendered to the client side by JSF in order to be able to be updated (re-rendered) by JavaScript/Ajax again. So best is to put the <h:dataTable> in a <h:panelGroup> with that ID.
<h:panelGroup id="comments">
<h:dataTable rendered="#{not empty commentManager.comments}">
...
</h:dataTable>
</h:panelGroup>
See also:
Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
You need to modify your button:
<h:commandButton value="read" action="#{commentManager.findByTopic}">
<f:ajax render="comments" />
</h:commandButton>
This means, when the button is clicked, the action is executed, and the dataTable will be rendered and updated. This only works if the backing bean is at least view-scoped.

Resources