In JSF 2.0, coding Ajax is just like coding a normal HTML tag, it’s
extremely easy. In this tutorial, you will restructure the last JSF 2.0 hello world example, so that, when the button is clicked, it will make an Ajax request instead of submitting the whole form.
helloAjax.xhtml
In
this example, it make the button Ajaxable. When the button is clicked,
it will make an Ajax request to the server instead of submitting the
whole form.
In the <f:ajax> tag :
1. JSF 2.0 Page
A JSF 2.0 xhtml page with Ajax support.helloAjax.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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <h3>JSF 2.0 + Ajax Hello World Example</h3> <h:form> <h:inputText id="name" value="#{helloBean.name}"></h:inputText> <h:commandButton value="Welcome Me"> <f:ajax execute="name" render="output" /> </h:commandButton> <h2><h:outputText id="output" value="#{helloBean.sayWelcome}" /></h2> </h:form> </h:body> </html>
<h:commandButton value="Welcome Me"> <f:ajax execute="name" render="output" /> </h:commandButton> <h:outputText id="output" value="#{helloBean.sayWelcome}" />
- execute=”name” – Indicate the form component with an Id of “name” will be sent to the server for processing. For multiple components, just split it with a space in between, e.g execute=”name anotherId anotherxxId”. In this case, it will submit the text box value.
- render=”output” – After the Ajax request, it will refresh the component with an id of “output“. In this case, after the Ajax request is finished, it will refresh the <h:outputText> component.
2. ManagedBean
See the full #{helloBean} example.
HelloBean.java
package com.mkyong.common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class HelloBean implements Serializable { private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSayWelcome(){ //check if null? if("".equals(name) || name ==null){ return ""; }else{ return "Ajax message : Welcome " + name; } } }
3. How it work?
Access the URL : http://localhost:8080/JavaServerFaces/helloAjax.jsf
Add caption Download Source Code
Download it – JSF-2-Ajax-Hello-World-Example.zip (8KB)
jianbin1205
ReplyDeletecheap uggs
north face outlet online
coach outlet online
uggs outlet
snapback hats
true religion outlet,true religion jeans,true religion kids,true religion jeans sale,true religion jeans for men,true religion jacket,true religion sale
cheap oakley sunglasses
moncler coats
ugg boots on sale
cyber monday
kobe bryants shoes 2015
polo ralph lauren uk
moncler outlet
lululemon outlet
prada outlet
chanel handbags
ugg uk outlet
toms outlet
canada goose outlet
the north face jackets
new england patriots
chicago blackhawks
michael kors outlet sale
new york giants
air force one shoes
nike air max 90
ugg boots
air jordan 11 free shipping
michael kors handbags
nike trainers uk
air jordan shoes
kansas city chiefs
chicago bulls