PrimeFaces.current() is a Java utility for the current request, e.g. to execute a script in the browser, to update components or to add parameters to Ajax callbacks. In this example the save action adds callback parameters, which the oncomplete handler reads from args and writes to the p:log console, and executes a script that logs a message.
<style>
.p-log {
left: 600px;
top: 100px;
}
</style>
<script>
function handleComplete(xhr, status, args) {
if (args.validationFailed) {
PrimeFaces.debug("Validation Failed");
} else {
PrimeFaces.debug("Save:" + args.saved);
PrimeFaces.debug("FirstName: " + args.user.firstname + ", Lastname: " + args.user.lastname);
}
}
</script>
<div class="card">
<h:form id="form">
<p:messages>
<p:autoUpdate />
</p:messages>
<div class="formgrid grid p-fluid">
<div class="field col-12 md:col-6">
<p:outputLabel for="firstname" value="Firstname"/>
<p:inputText id="firstname" value="#{requestContextView.user.firstname}" required="true"/>
</div>
<div class="field col-12 md:col-6">
<p:outputLabel for="surname" value="Lastname"/>
<p:inputText id="surname" value="#{requestContextView.user.lastname}" required="true"/>
</div>
</div>
<p:commandButton value="Save" action="#{requestContextView.save}" styleClass="mt-2"
oncomplete="handleComplete(xhr, status, args)"/>
<p:log/>
</h:form>
</div>
package org.primefaces.showcase.view.misc;
import org.primefaces.PrimeFaces;
import org.primefaces.showcase.domain.User;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
@Named
@RequestScoped
public class RequestContextView {
private User user;
@PostConstruct
public void init() {
user = new User();
if (!FacesContext.getCurrentInstance().isPostback()) {
PrimeFaces.current().executeScript("PrimeFaces.info('This message is added from backing bean.')");
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void save() {
PrimeFaces.current().ajax().addCallbackParam("saved", true); //basic parameter
PrimeFaces.current().ajax().addCallbackParam("user", user); //pojo as json
//execute javascript oncomplete
PrimeFaces.current().executeScript("PrimeFaces.info('Hello from the Backing Bean');");
//update panel
PrimeFaces.current().ajax().update("form:panel");
//scroll to panel
PrimeFaces.current().scrollTo("form:panel");
//add facesmessage
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Success", "Success"));
}
}
package org.primefaces.showcase.domain;
import java.io.Serializable;
public class User implements Serializable {
private String firstname;
private String lastname;
private Integer age;
private String street;
private String city;
private String postalCode;
private String info;
private String email;
private String phone;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}