PrimeFaces.current() is a helper with various utilities.
<style>
.ui-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>
<h:panelGrid columns="2" cellpadding="7" id="panel">
<p:outputLabel for="firstname" value="Firstname:"/>
<p:inputText id="firstname" value="#{requestContextView.user.firstname}" required="true"/>
<p:outputLabel for="surname" value="Lastname:"/>
<p:inputText id="surname" value="#{requestContextView.user.lastname}" required="true"/>
</h:panelGrid>
<p:commandButton value="Save" action="#{requestContextView.save}" styleClass="mt-2"
oncomplete="handleComplete(xhr, status, args)"/>
<p:log/>
</h:form>
</div>
@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"));
}
}
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;
}
}