jsf - 为什么 GlassFish 5.1.0 中的 JSF 将我的 @ViewScoped CDI bean 的 ID 属性设置为 null?
问题描述
我正在从 GlassFish 4.1.2 升级到 GlassFish 5.1.0,遇到了一个问题,即 JSF 生命周期的更新模型阶段将我的 @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;
}
}
下面演示了这个问题:
- 将浏览器指向 LocationEditor2.xhtml?id=1
- 选择[更新]按钮。
- updateLocation 方法记录“位置 ID:1”
- 选择 [更新] 按钮
- updateLocation 方法记录“位置 ID:空”
我验证了视图没有被重新创建,并且在更新模型 JSF 阶段调用 setId(Long id) 方法的值为 null。
为什么 JSF 更新模型阶段在第二次和另外的回发期间将我的实体 ID 设置为空?
解决方案
问题的根本原因是在 JSF 生命周期的更新模型阶段调用我的实体上的 setId(null) 方法。我使用 setId() 方法中的 StackTraceElement[] 数组来查看调用方法,并确定 UIViewParameter 类的 updateModel 方法是罪魁祸首。
经过一番谷歌搜索,我发现了OmniFaces viewParam Showcase,它提供了“无状态模式,以避免在回发时进行不必要的转换、验证和模型更新”。在更改我的代码以使用 OmniFaces viewParam 组件后,JSF 更新模型基础结构在回发期间停止在我的实体上调用 setId() 方法,并且一切都像在 GlassFish 4.1.2 中一样工作。
解决方案是从标准 f:viewParam 转移到 OmniFaces o:viewParam。
推荐阅读
- javascript - 防止有条件地选择输入值更改
- c - 如果重复使用,将面临 atoi 功能的问题
- javascript - 使用 Angular 5 routerLink 进行路由时,联系人组件中的主页滑块或谷歌地图未加载
- arrays - Bash 脚本:如果 VARIABLE 不是数组,${VARIABLE[number]} 是什么意思?
- matlab - 是否可以在 Matlab 的bayesopt 中保存检查点?
- vue.js - VueJS 在用户输入数据时禁用排序
- excel - Power Query - 从日期编号和时间列创建日期
- sql-server - SSRS 选择性分页符
- hadoop - 汇合:Hdfs 沉入 avro 格式,但在 hive 中读取 avro 文件时,我的时间比“时区”提前 5:30 小时:“亚洲/加尔各答”
- angular - 这是什么结构,我该如何使用它?