我正在从GlassFish 4.1.2升级到GlassFish 5.1.0的过程中,遇到一个问题,其中JSF生命周期的Update Model阶段将@ViewScoped CDI支持bean的ID属性的值设置为null 。以下是我的简化视图。
<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>
以下是我的简化CDI支持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;
}
}
以下内容说明了该问题:
我已验证未重新创建视图,并且在更新模型JSF阶段中有人在调用setId(Long id)方法,其值为null。
为什么在第二次和附加回发期间,JSF更新模型阶段将我的实体的ID设置为null?
答案 0 :(得分:0)
此问题的根本原因是在JSF生命周期的更新模型阶段,有人在我的实体上调用setId(null)方法。我在setId()方法中使用了StackTraceElement []数组来检查调用方法,并确定UIViewParameter类的updateModel方法是罪魁祸首。
经过一番谷歌搜索,我发现OmniFaces viewParam Showcase提供了“无状态模式,避免了对回发进行不必要的转换,验证和模型更新”。将代码更改为使用OmniFaces viewParam组件后,JSF Update Model基础结构在回发期间停止在我的实体上调用setId()方法,并且一切都与GlassFish 4.1.2一样。
解决方案是从标准f:viewParam迁移到OmniFaces o:viewParam。