Skip to main content

Mixing Ajax and full requests in JSF 2.0

Posted by driscoll on October 1, 2009 at 6:20 PM PDT

JSF 2.0 makes ajax pretty easy - but it can't hide everything from you... It's tempting to just add a few ajax tags into your page, and not worry too much about interactions - here's one example of a problem you may run into.

Let's say you've got a page with an input text, and a command button - like this:

   1 <h:form>
   2 <h:inputText value="#{blah.blah}">
   3 </h:inputText>
   4 <h:commandButton/>
   5 </h:form>

Now, we decide to add an ajax tag:

   1 <h:form>
   2 <h:inputText value="#{blah.blah}">
   3 <f:ajax event="blur"/>
   4 </h:inputText>
   5 <h:commandButton/>
   6 </h:form>

Can you spot what's wrong with this example? When we push the button, we're also blurring the inputText. That means that the ajax request is sent - but then, almost immediately, that request is canceled as the whole page is reloaded.

Is this a bad thing? For this simple example, not so much. There's going to be a broken connection - and that can be a grim problem for a large server, especially if you start getting one on each page, for each use.

But the real issue is that you've just set up a race condition. Imagine instead you did this:


   1 <h:form>
   2 <h:inputText value="#{blah.blah}">
   3 <f:ajax event="blur" listener="#{bean.somethingthatchangesstate}"/>
   4 </h:inputText>
   5 <h:commandButton/>
   6 </h:form>

Now we've got a real problem from that race condition - did the listener execute? Maybe. Maybe is never a good answer in software.

So - what to do?

Probably the best solution is also the simplest:

   1 <h:form>
   2 <h:inputText value="#{blah.blah}">
   3 <f:ajax event="blur" listener="#{bean.somethingthatchangesstate}"/>
   4 </h:inputText>
   5 <h:commandButton>
   6 <f:ajax render="@form">
   7 </h:commandButton>
   8 </h:form>

Switching to ajax for the commandButton will now provide a predictable call sequence.

One more issue: When the two connections are submitted simultaneously, an error alert may be produced. I just updated that error to say: "The Http Transport returned a 0 status code. This is usually the result of mixing ajax and full requests. This is usually undesired, for both performance and data integrity reasons." What happens if you want to do this? Well, the error alert only shows up under two conditions, both of which must be true - the Project Stage must be Development, and there must be no error listener set. So, if you're really sure you want to mix ajax and full requests, despite what I said above, just set up an error listener for your ajax code - you'll want to anyway for a production environment.

As always, if you have questions, please ask in the comments.

Related Topics >>


Mixing Ajax and full requests




st1\:*{behavior:url(#ieooui) }

/* Style Definitions */
{mso-style-name:"Normale Tabelle";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
font-family:"Times New Roman";

What is the best way to handle the situation that the commandButton can't get an <f:ajax…/> tag, because it navigates to another page (it may stay on the same page, if an error in the user input has been detected - that's when I noticed the Http transport error.)?
Is there a way to assure, that the server logic of the text fields ajax request has been completed before the processing of the commandButton logic starts?

Using render="@form" and @Pattern messages


Is it possible to use "f:ajax render="@form"" and display "@Pattern(...,message="error...")" messages?

Thanks & Regards

---- h:form
h:outputText value="Fied 1"
h:inputText id="field1" value="#{teste.field1}"
f:ajax event="blur" render="wf1"
h:message id="wf1" for="field1" styleClass="error"

h:outputText value="field 2"
h:inputText id="field2" value="#{teste.field2}"
f:ajax event="blur" render="wf2"
h:message id="wf2" for="field2" styleClass="error"

h:outputText value="Just a check"
h:outputText id="checknumber" value="#{teste.count}"

!-- f:ajax render="@form"/> when used no messages are displayed -->

package teste;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.Pattern;

@ManagedBean(name = "teste")
public class Teste implements Serializable {
private final String erro="Cannot be empty";
private static final long serialVersionUID = 7095529894728768608L;
@Pattern(regexp="(.+)", message=erro)
private String field1;

@Pattern(regexp="(.+)", message=erro)
private String field2;

private int count=0;
public Teste(){

public String getField1() {
System.out.println("getField1 : "+field1);
return field1;

public void setField1(String str){
System.out.println("setField1 : "+str);
public int getCount(){
return count;

public String getField2() {
System.out.println("getField2 : "+field2);
return field2;

public void setField2(String str){
System.out.println("setField2 : "+str);

Not really the forum for this question

Hi -

While I like to be helpful, I don't like the idea of answering random questions on my blog's comments - the comments are better suited for questions about the subject I'm posting about.

For questions unrelated to my blog's subject (but still related to JSF 2), please send them to - bean validation (which I'm not that familiar with) would fall into that category. I probably can't answer your question, but someone there will be able to.