f:ajax not working in JSF

Problem is that in JavaServer Faces application ajax request are just not working. o:ajax in openfaces nor f:ajax in plain JSF 2.0 is not sending any requests to server. For testing can be used simple echo application.

<h:form>
<h:inputText value="#{ajaxTestBean.testValue}" >
  <f:ajax event="keyup" render="text"/>
</h:inputText>
<h:outputText id="text" value="#{ajaxTestBean.testValue}" />
</h:form>

Normally this should work without problems but there is little catch. <h:head></h:head> tag must be included in order for ajax to work properly. Definind h:head includes some more needed javascripts to complete everything. Try this code below to get it working:
index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:head>
  <title>AjaxTest</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{ajaxTestBean.testValue}" >
  <f:ajax event="keyup" render="text"/>
</h:inputText>
<h:outputText id="text" value="#{ajaxTestBean.testValue}" />
</h:form>
</h:body>
</f:view>
</html>

AjaxTestBean.java

package com.ajaxtest; 

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped; 

@ManagedBean(name = "ajaxTestBean")
@SessionScoped
public class AjaxTestBean {
private String testValue = "default";

public String getTestValue() {
  return testValue;
} 

public void setTestValue(String testValue) {
   this.testValue = testValue;
 }
}

10 thoughts on “f:ajax not working in JSF

  1. thank you very much for trick! it could take me infinity to catch this up if I don’t stumble on your post.

  2. sorry, missed head tag in my previous message forced me to check you site on XSS leaks =)

  3. Thanks a lot!
    I spent last two days trying to find out why ajax doesn’t work. I couldn’t sleep last night and I was about to give up programming in Java.

  4. I have a problem I don’t seem to find a solution to. The setter for the ‘testValue’ is called only for the first character. It follows the getters will return null every time the ‘keyup’ event takes place.

    (I’ve used the code you’ve posted)

    Any help is much appreciated

  5. Thanks a lot!! Saved my day!
    ” tag must be included in order for ajax to work properly”

Leave a Reply

Your email address will not be published.


*