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>
package org.primefaces.showcase.view.misc;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.PrimeFaces;
import org.primefaces.showcase.domain.User;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
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"));
}
}