f:ajax è un tag JSF che aggiunge il meccanismo delle request asincrone a molti componenti UI.
Per passare dei parametri viene aggiunto il tag f:param
pagina con supporto ajax:
ProvaAjax.xhmtl
<!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”
xmlns:ui=”http://java.sun.com/jsf/facelets”
xmlns:p=”http://primefaces.org/ui”
xmlns:c=”http://java.sun.com/jsp/jstl/core”>
<h:head/>
<h:body>
<h:form>
<input type=”hidden” name=”id2″ id=”id2″ value=”55″ />
<h:commandLink value=”ssss”>
<f:param name=”id” value=”44″ />
<f:ajax listener=”#{galleriaBean.countListener}”>
</f:ajax>
</h:commandLink>
</h:form>
</h:body>
</html>
<html xmlns=”http://www.w3.org/1999/xhtml”
xmlns:f=”http://java.sun.com/jsf/core”
xmlns:h=”http://java.sun.com/jsf/html”
xmlns:ui=”http://java.sun.com/jsf/facelets”
xmlns:p=”http://primefaces.org/ui”
xmlns:c=”http://java.sun.com/jsp/jstl/core”>
<h:head/>
<h:body>
<h:form>
<input type=”hidden” name=”id2″ id=”id2″ value=”55″ />
<h:commandLink value=”ssss”>
<f:param name=”id” value=”44″ />
<f:ajax listener=”#{galleriaBean.countListener}”>
</f:ajax>
</h:commandLink>
</h:form>
</h:body>
</html>
nel tag f:ajax:
– execute=”name”, indica che il componente con id “name” deve essere mandato al server.
– render=”output”, indica che il componente con id “output” dovrà, dopo la request ajax, essere aggiornato.
– listener=“#{provaTag.handleEvent}”, indica il metodo del bean da chiamare.
GalleriaBean.java
package com;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
@ManagedBean
@SessionScoped
public class GalleriaBean {
public void countListener(AjaxBehaviorEvent event) {
System.out.println(“test:”);
String id = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get(“id”);
System.out.println(“id:” + id);
}
}
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
@ManagedBean
@SessionScoped
public class GalleriaBean {
public void countListener(AjaxBehaviorEvent event) {
System.out.println(“test:”);
String id = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get(“id”);
System.out.println(“id:” + id);
}
}
aprendo l’indirizzo: http://localhost:8080/JSFProject/faces/ProvaAjax.xhtml
cliccando sul link “sss” si entra dentro il listener che scrive sulla console: