<a4j:ajax oncomplete> is not triggered while <a4j:support oncomplete> did - ajax

I'm trying to trigger a javascript function using the a4j:ajax method. We are migrating from version 3 to version 4, and have also been upgrading spring and JSF. Previously, this worked fine:
<a4j:support event="change" oncomplete="callJavascriptMethod()">
With version 4 of RichFaces, I'm trying to do the same, calling a javascript function everytime a dropdown list is beeing changed, but I have changed to use:
<a4j:ajax event="change" oncomplete="callJavascriptMethod()">
I have also tried something like this just to see if the event's are triggered:
<h:commandButton id="test" value="Test me!">
<a4j:ajax event="click" oncomplete="console.log('something');" />
</h:commandButton>
So far without luck. Any ideas?

The commandButton example above is working in our migration. We had a lot of issues with a4j:ajax in 4.3.2 but upgrading to 4.3.4 seemed to fix most of the issues, so if that is an option and hasn't been done I would recommend getting the latest version for RichFaces 4.

Related

How to pass parameters with JSF AJAX response as native ajax requests

I want to send back (add some parameter) some value with JSF ajax response as native JSF ajax and jQuery ajax. How to do it? I'm using JSF 2.2, if that's relevant.
You can use <f:param> inside buttons.
<h:commandButton value="Send">
<f:param name="c" value="#{bean.value}"/>
<f:ajax execute="#this" listener="#{bean.listener}"/>
</h:commandButton>

javax.servlet.ServletException the request doesn't contain a multipart/form-data or multipart/mixed stream

I've the below form which uploads a file via <h:inputFile> and <f:ajax>.
<h:form enctype="multipart/form-data">
<h:inputFile id="file" value="#{bean.file}" />
<h:commandButton value="Upload" action="#{bean.upload}">
<f:ajax execute="file" render="file" />
</h:commandButton>
</h:form>
When I submit it, I get a JavaScript alert with the below error information:
serverError: class javax.servlet.ServletException the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded;charset=UTF-8
Is there any solution for this problem? I'm using Mojarra 2.2.11.
I reproduced it. It is a bug in Mojarra, introduced in 2.2.9 as side effect of the fix for issue 3129. Your problem is already reported as issue 3765. Basically: uploading files with ajax is broken since Mojarra 2.2.9 and there are no workarounds (at least not without hacking in the code).
For now, you need to downgrade to Mojarra 2.2.8 until they release the version with the fix.

Ajax listener not working with inputFile

First of all, I'm using JSF 2.2.7 and GlassFish 4 in NetBeans 8.0.
Second, sorry if I left any useful info out. This is my first question in here, so I may forget something. Also, english isn't my first language. Sorry for typos/grammar.
So, here's my problem: I'm uploading files with h:inputFile and I need an ajax listener for it.
After noticing that it wasn't working as I expected, I decided to do some testing with a listener that prints "Test", but it's never fired with h:inputFile. I've tried with h:commandButton and h:commandLink and they both work fine.
For further testing, I created a new project using the same test code and everything works, including the listener in h:inputFile.
What I want to know is why it doesn't work in my main project and why it works in the test project. What am I missing here? And how can I make it work in my main project?
Here are some (but not all, since I lost track) things that I've tried/checked so far, after looking for answers:
There are no nested forms. The said xhtml is in a ui:composition, but even taking that into account, there are no nested forms.
Its form has enctype="multipart/form-data"
I've tried changing the method to uploadTest(AjaxBehaviorEvent event) and the listener to listener="#{submitFormBean.uploadTest}" [without the ()]
I've played with the various event="..." and execute="..." (didn't think it would solve my problem, but, well, I had to try)
The code follows below:
xhtml
<h:form>
<h:commandButton id="btnTest">
<f:ajax listener="#{submitFormBean.uploadTest()}"/>` --> works in both projects`
</h:commandButton>
</h:form>
<h:form>
<h:commandLink id="lnkTest" value="Test">
<f:ajax listener="#{submitFormBean.uploadTest()}"/>` --> works in both projects`
</h:commandLink>
</h:form>
<h:form enctype="multipart/form-data">
<h:inputFile id="fileTest">
<f:ajax listener="#{submitFormBean.uploadTest()}"/>` --> works only in test project`
</h:inputFile>
</h:form>
bean
#ViewScoped
#ManagedBean(name = "submitFormBean")
public class SubmitFormBean{
public SubmitFormBean() {
}
public void uploadTest(){
System.out.println("Test");
}
}
Thanks in advance!
I was about to do what I needed in jQuery, but first I decided to try the following and it solved my problem:
right click project/properties/libraries
in the compile tab, I removed and readded the JSF 2.2.7 .jar
It was already there, but for some reason wasn't working. I still don't know what went wrong, but it's working now.
With multipart/form-data in JSF form, you have to do additional configuration to make it works.
You can refer to other questions:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated
JSF 2.0 File upload
And
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null

ui:include in richfaces 4 only gets updated on second click

I am new to Richfaces4 and JSF2 and having lots of issues. Same code start to work suddenly and then it breaks. Currently I have a ui:include tag that update the page when the a4j:commandLink is clicked. Using debugging feature of eclipse I can see that the methods are being called and correct value is stored in the bean but page never gets updated until I click on it the second time. It seems the page is updated before the values are set in the backing bean. Following is my code. The same thing was working in MyFaces 2.0.2 before I updated to MyFaces 2.1.3.
<a4j:outputPanel id="pagePanel" layout="block">
<ui:include src="#{panelMenu.currentPage}"></ui:include>
</a4j:outputPanel>
<h:panelGrid>
<p><a4j:commandLink value="View Member" render="pagePanel" execute="pagePanel">
<a4j:param value="/pages/group_member.xhtml" assignTo="#{panelMenu.currentPage}"/>
</a4j:commandLink></p>
<p><a4j:commandLink value="View/Edit Groups" render="pagePanel" execute="pagePanel">
<a4j:param value="/pages/group_detail.xhtml" assignTo="#{panelMenu.currentPage}"/>
</a4j:commandLink></p>
</h:panelGrid>

Intercept JSF-validations

I am starting to get to know JSF and I have been able to add some standard validators that show up in messages that I have defined, but they only validate when I click the button to submit the form where the components that need to be validated are in. I would like those components to be validated already on the moment that I click outside them (for example with onblur), but I don't know which method I have to call in the onblur-property to execute the validator of that element? The code I have at this moment is:
<h:form>
<h:outputText value="#{msgs.name}"/><br/>
<h:inputText id="name" value="#{user.name}" label="#{msgs.name}" required="true" requiredMessage="#{msgs.name_required}"/>
<h:message for="name" styleClass="error_single_message"/>
<br/><br/>
<h:outputText value="#{msgs.password}"/><br/>
<h:inputText id="password" value="#{user.password}" label="#{msgs.password}" required="true" requiredMessage="#{msgs.password_required}"/>
<h:message for="password" styleClass="error_single_message"/>
<br/><br/>
<div class="error_overview_messages">Overview errors
<h:messages showDetail="#{true}" showSummary="#{false}"/>
</div><br/>
<h:commandButton value="Log_on" action="logged_in"/>
</h:form>
JSF validations happens server side, so you will essentially need to have a submit triggered to have server side code activated.
If you are not interested in that, you need to activate some specific Java script.
If you are using JSF2, you can accomplish what you are looking for by adding Richfaces to your build and using Richfaces client side validation:
Richfaces Client Side Validation
It is a great new feature in their v4 release that compiles standard jsf validators and JSR-303 bean validation to javascript that can be executed client side. You can trigger the validations on any standard javascript event (blur, click, keyup).

Resources