我有一个程序化的Dynamic TreeTable,它被创建并添加到表单面板中。此问题仅在Mojarra中可重现。
该代码仅适用于
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
或
<context-param>
<param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
<param-value>/treetableprogram.xhtml</param-value>
</context-param>
最小,完整和可验证 - primefaces 6.2,jsf 2.3.3 https://github.com/ravihariharan/primefaces-test-treetable.git
treetableprogram.xhtml,BasicView1.java
基于PrimeFaces展示示例:https://www.primefaces.org/showcase/ui/data/treetable/basic.xhtml
@PostConstruct
public void init() {
root = service.createDocuments();
TreeTable table = new TreeTable();
table.setId("treeTableId");
table.setVar("document");
table.setValueExpression("value", createValueExpression("#{ttBasicView1.root}", TreeNode.class));
Column column = new Column();
column.setId("col1");
OutputLabel label = new OutputLabel();
label.setId("ol1");
label.setValueExpression("value", createValueExpression("#{document.name}", String.class));
column.getChildren().add(label);
table.getChildren().add(column);
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = context.getViewRoot();
viewRoot.findComponent("form:mainPanel").getChildren().add(table);
}
问题
参考网址:JSF State saving and custom components with dynamically added children
MyFaces实施不会出现此问题。 https://github.com/ravihariharan/primefaces-test-myfaces.git
我不确定这是否是Mojarra部分状态保存问题或PrimeFaces导致的错误。
BasicView1.java
package org.primefaces.showcase.view.data.treetable;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import org.primefaces.component.column.Column;
import org.primefaces.component.outputlabel.OutputLabel;
import org.primefaces.component.treetable.TreeTable;
import org.primefaces.model.TreeNode;
import org.primefaces.showcase.domain.Document;
import org.primefaces.showcase.service.DocumentService;
@ManagedBean(name = "ttBasicView1")
@ViewScoped
public class BasicView1 implements Serializable {
private String testString = "";
private TreeNode root;
private Document selectedDocument;
@ManagedProperty("#{documentService}")
private DocumentService service;
@PostConstruct
public void init() {
root = service.createDocuments();
TreeTable table = new TreeTable();
table.setId("treeTableId");
table.setVar("document");
table.setValueExpression("value", createValueExpression("#{ttBasicView1.root}", TreeNode.class));
Column column = new Column();
column.setId("col1");
OutputLabel label = new OutputLabel();
label.setId("ol1");
label.setValueExpression("value", createValueExpression("#{document.name}", String.class));
column.getChildren().add(label);
table.getChildren().add(column);
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = context.getViewRoot();
viewRoot.findComponent("form:mainPanel").getChildren().add(table);
}
public TreeNode getRoot() {
return root;
}
public void setService(DocumentService service) {
this.service = service;
}
public Document getSelectedDocument() {
return selectedDocument;
}
public void setSelectedDocument(Document selectedDocument) {
this.selectedDocument = selectedDocument;
}
public static ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(),
valueExpression, valueType);
}
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
}
Document.java
package org.primefaces.showcase.domain;
import java.io.Serializable;
public class Document implements Serializable, Comparable<Document> {
private String name;
private String size;
private String type;
public Document(String name, String size, String type) {
this.name = name;
this.size = size;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//Eclipse Generated hashCode and equals
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((size == null) ? 0 : size.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Document other = (Document) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (size == null) {
if (other.size != null)
return false;
} else if (!size.equals(other.size))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override
public String toString() {
return name;
}
public int compareTo(Document document) {
return this.getName().compareTo(document.getName());
}
}
treetableprogram.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>PrimeFaces Test</title>
</h:head>
<h:body>
<h1>#{ttBasicView1.testString}</h1>
<h:form id="form">
<p:panel id="mainPanel" header="">
</p:panel>
</h:form>
</h:body>
</html>