I am in the process of upgrading from GlassFish 4.1.2 to GlassFish 5.1.0 and encountered an issue where the Update Model phase of the JSF lifecycle is setting the value of the ID property of my @ViewScoped CDI backing beans to null. The following is my simplified view.
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/modena.xhtml">
<ui:define name="title">Location Editor2</ui:define>
<ui:define name="content">
<f:metadata>
<f:viewParam name="id" value="#{locationEditor2.location.id}" />
<f:viewAction action="#{locationEditor2.setLocationById}" />
</f:metadata>
<h:form id="location-editor-form">
<p:commandButton value="Update" styleClass="RaisedButton"
action="#{locationEditor2.updateLocation}" update="@form" />
</h:form>
</ui:define>
</ui:composition>
The following is my simplified CDI backing bean.
import java.io.Serializable;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.elliottlogic.elis.ejb.location.Location;
import com.elliottlogic.elis.ejb.location.LocationManager;
import com.elliottlogic.elis.ejb.security.AuthorizationException;
import com.elliottlogic.elis.wa.core.ExceptionTools;
/**
* The <code>LocationEditor2</code> class TODO Complete Type Description
*
* @author jre
* @version $Id: $
*
*/
@Named
@ViewScoped
public class LocationEditor2 implements Serializable {
final static Logger logger = LoggerFactory.getLogger(LocationEditor2.class.getName());
static final long serialVersionUID = 1L;
private Location location = new Location();
@EJB
private LocationManager locationManager;
/**
* Configures model using persisted values.
*
* @throws AuthorizationException
*/
public void setLocationById() throws AuthorizationException {
try {
location = locationManager.readLocationByID(location.getId());
} catch (EJBException e) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Location not found.", "Location not found."));
ExceptionTools.unwrapEJBException(FacesContext.getCurrentInstance(), e);
}
}
/**
* Saves the changes to the location.
*
* @return
*/
public String updateLocation() {
logger.debug("Location ID: {}", location.getId());
return null;
}
/**
* @return the location
*/
public Location getLocation() {
return location;
}
/**
* @param location
* the location to set
*/
public void setLocation(Location location) {
this.location = location;
}
}
The following demonstrates the issue:
- Point Browser at LocationEditor2.xhtml?id=1
- Select the [Update] button.
- The updateLocation method logs "Location ID: 1"
- Select the [Update] button
- The updateLocation method logs "Location ID: null"
I verified that the view is not being recreated and that something is calling the setId(Long id) method with a value of null during the Update Model JSF phase.
Why is the JSF Update Model phase setting the ID of my entity to null during the second and addition postbacks?