更改节点上的JSF树验证表单
我有一个类文档,用于创建包含文档对象列表的树更改节点上的JSF树验证表单,jsf,primefaces,tree,Jsf,Primefaces,Tree,我有一个类文档,用于创建包含文档对象列表的树 public class Document implements Serializable { private String name; private String size; private List<Field> fields; public Document(String name, String size, String type) { this.name = nam
public class Document implements Serializable {
private String name;
private String size;
private List<Field> fields;
public Document(String name, String size, String type) {
this.name = name;
this.size = size;
}
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 List<Field> getFields() {
return fields;
}
public void setFields(List<Field> fields) {
this.fields = fields;
}
}
公共类文档实现可序列化{
私有字符串名称;
私有字符串大小;
私有列表字段;
公共文档(字符串名称、字符串大小、字符串类型){
this.name=名称;
这个。大小=大小;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getSize(){
返回大小;
}
公共void setSize(字符串大小){
这个。大小=大小;
}
公共列表getFields(){
返回字段;
}
公共无效设置字段(列表字段){
this.fields=字段;
}
}
我还有一个字段类,用于在文档中存储相关信息
public class Field implements Serializable {
private int fieldIndex;
private String label;
private String value;
private List<Values> list;
public Field() {
}
public int getFieldIndex() {
return fieldIndex;
}
public void setFieldIndex(int fieldIndex) {
this.fieldIndex = fieldIndex;
}
public Field(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<Values> getList() {
return list;
}
public void setList(List<Values> list) {
this.list = list;
}
}
公共类字段实现可序列化{
私有int字段索引;
私有字符串标签;
私有字符串值;
私人名单;
公共领域(){
}
public int getFieldIndex(){
返回字段索引;
}
public void setFieldIndex(int fieldIndex){
this.fieldIndex=fieldIndex;
}
公共字段(字符串标签){
this.label=标签;
}
公共字符串getLabel(){
退货标签;
}
公共void setLabel(字符串标签){
this.label=标签;
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
}
我的ManagedBean创建一个包含一些文档的树,并为每个文档存储一些数据。当我选择树节点时,它会显示一个动态表单,每个字段都有输入值
@ManagedBean(name="treeSelectionView")
@ViewScoped
public class SelectionView implements Serializable {
private TreeNode root1;
private TreeNode selectedNode;
private String email;
private List<Field> fields;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@PostConstruct
public void init() {
TreeNode root = new DefaultTreeNode(new Document("Files", "-", "Folder"), null);
TreeNode documents = new DefaultTreeNode(new Document("Documents", "-", "Folder"), root);
Field f1=new Field();
Field f2=new Field();
Field f3=new Field();
f1.setLabel("email");
f1.setValue("");
f2.setLabel("doc");
f2.setValue("");
f3.setLabel("otro");
f3.setValue("");
List<Field> fields=new ArrayList<Field>();
fields.add(f1);
fields.add(f2);
fields.add(f3);
List<Field> fields1=new ArrayList<Field>();
f1=new Field();
f2=new Field();
f3=new Field();
f1.setLabel("email");
f1.setValue("");
f2.setLabel("doc");
f2.setValue("");
f3.setLabel("otro");
f3.setValue("");
fields1.add(f1);
fields1.add(f2);
fields1.add(f3);
List<Field> fields2=new ArrayList<Field>();
f1=new Field();
f2=new Field();
f3=new Field();
f1.setLabel("email");
f1.setValue("");
f2.setLabel("doc");
f2.setValue("");
f3.setLabel("otro");
f3.setValue("");
fields2.add(f1);
fields2.add(f2);
fields2.add(f3);
//Documents
Document d1= new Document("Expenses.doc", "30 KB", "Word Document");
Document d2=new Document("Resume.doc", "10 KB", "Word Document");
Document d3=new Document("RefDoc.pages", "40 KB", "Pages Document");
d1.setFields(fields);
d2.setFields(fields1);
d3.setFields(fields2);
TreeNode expenses = new DefaultTreeNode("document",d1, documents);
TreeNode resume = new DefaultTreeNode("document", d2, documents);
TreeNode refdoc = new DefaultTreeNode("document",d3 , documents);
documents.setExpanded(true);
root1 = root;
root1.setExpanded(true);
}
public void onNodeDocumentSelect(NodeSelectEvent nodeSelected) {
// fields=((Document)nodeSelected.getTreeNode().getData()).getFields();
fields=((Document)selectedNode.getData()).getFields();
}
public TreeNode getRoot1() {
return root1;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
public List<Field> getFields() {
return fields;
}
public void setFields(List<Field> fields) {
this.fields = fields;
}
}
@ManagedBean(name=“treeSelectionView”)
@视域
公共类SelectionView实现可序列化{
私有树节点1;
私有树节点选择节点;
私人字符串电子邮件;
私有列表字段;
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
@施工后
公共void init(){
TreeNode root=new DefaultTreeNode(新文档(“文件”)、“-”和“文件夹”),null;
TreeNode documents=new DefaultTreeNode(新文档(“文档”、“-”、“文件夹”)、根目录);
字段f1=新字段();
字段f2=新字段();
字段f3=新字段();
f1.设置标签(“电子邮件”);
f1.设定值(“”);
f2.设置标签(“文件”);
f2.设定值(“”);
f3.设置标签(“otro”);
f3.设定值(“”);
列表字段=新的ArrayList();
字段。添加(f1);
字段。添加(f2);
字段。添加(f3);
列表字段1=新的ArrayList();
f1=新字段();
f2=新字段();
f3=新字段();
f1.设置标签(“电子邮件”);
f1.设定值(“”);
f2.设置标签(“文件”);
f2.设定值(“”);
f3.设置标签(“otro”);
f3.设定值(“”);
字段1.添加(f1);
字段1.添加(f2);
字段1.添加(f3);
列表字段2=新的ArrayList();
f1=新字段();
f2=新字段();
f3=新字段();
f1.设置标签(“电子邮件”);
f1.设定值(“”);
f2.设置标签(“文件”);
f2.设定值(“”);
f3.设置标签(“otro”);
f3.设定值(“”);
字段2.添加(f1);
字段2.添加(f2);
字段2.添加(f3);
//文件
文档d1=新文档(“Expenses.doc”、“30KB”、“Word文档”);
文档d2=新文档(“Resume.doc”、“10KB”、“Word文档”);
文件d3=新文件(“参考文件页”,“40 KB”,“文件页”);
d1.设置字段(字段);
d2.设置字段(字段1);
d3.设置字段(字段2);
TreeNode费用=新的默认TreeNode(“文件”,d1,文件);
TreeNode resume=新的默认TreeNode(“文件”,d2,文件);
TreeNode refdoc=新的默认TreeNode(“文档”,d3,文档);
文件。setExpanded(true);
root1=根;
root1.setExpanded(true);
}
NodeDocumentSelect上的公共无效(NodeSelectEvent nodeSelected){
//fields=((文档)节点已选中。GetTreNode().getData()).getFields();
fields=((文档)selectedNode.getData()).getFields();
}
公共树节点getRoot1(){
返回根1;
}
公共树节点getSelectedNode(){
返回selectedNode;
}
公共无效集合selectedNode(TreeNode selectedNode){
this.selectedNode=selectedNode;
}
公共列表getFields(){
返回字段;
}
公共无效设置字段(列表字段){
this.fields=字段;
}
}
我的JSF看起来像
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Default title</title>
</h:head>
<h:body>
<h:panelGrid columns="2">
<h:form id="treeForm">
<p:panel id="panel22" header="Documents" style="height:400px">
<p:growl id="msgs" showDetail="true" />
<p:tree value="#{treeSelectionView.root1}" var="doc" selectionMode="single" selection="#{treeSelectionView.selectedNode}" >
<p:ajax process="@this" event="select" update=":myForm:dymanicForm" listener="#{treeSelectionView.onNodeDocumentSelect}" />
<p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed">
<h:outputText value="#{doc.name}"/>
</p:treeNode>
<p:treeNode type="document" icon="ui-icon-document" >
<h:outputText value="#{doc.name}" />
</p:treeNode>
</p:tree>
</p:panel>
</h:form>
<h:form id="myForm">
<p:panel id="panel222" header="Info Doccs" style="height:400px">
<p:panel id="dymanicForm" >
<ui:repeat value="#{treeSelectionView.fields}" var="componentMetadata">
<h:panelGrid columns="3">
<h:outputText value="#{componentMetadata.label}"/>:
<h:inputText id="field" value="#{componentMetadata.value}"
required="true" label="#{componentMetadata.label}"/>
<h:message for="field" style="color:red" /></h:panelGrid>
</ui:repeat>
</p:panel>
<h:commandButton value="Submit" action="result" />
</p:panel>
</h:form>
</h:panelGrid>
</h:body>
</html>
默认标题
:
当我按下submit按钮时,值被提交并存储在每个字段中,验证也被触发。但我真的需要删除提交按钮,并在每个节点失去焦点时验证表单。例如,如果我在第一个节点中,则仅当我失去焦点时才验证表单,但如果表单验证失败,则我需要留在该节点中
真的很感谢你能给我的任何帮助。
事先非常感谢。我想了解一个类似的观点,我花了很长时间才弄明白
诀窍是不在
上声明选择
属性,如果我在您提交信息时理解您的问题,如果一切正常,您需要关闭树,如果没有,您需要让树open@Neel我提交了答案,请看一看:)
<h:form>
<p:layout fullPage="false" stateful="false" style="height:400px">
<p:layoutUnit position="center">
<p:tree id="tree" var="data" nodeVar="node" value="#{testTreeBean.root}"
selectionMode="single" dynamic="true" animate="true" highlight="true"
style="border: 0">
<p:ajax event="select" listener="#{testTreeBean.onSelect}" process="@form"
update="@this @form:details" />
<p:ajax event="expand" process="@this" />
<p:ajax event="collapse" process="@this" />
<p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed">
<p:outputPanel id="node">
<h:outputText value="#{data.someText1} - #{data.someText2}" />
</p:outputPanel>
</p:treeNode>
</p:tree>
</p:layoutUnit>
<p:layoutUnit position="east" size="65%" minSize="150">
<p:outputPanel id="details" style="padding: 1em">
<p:panelGrid columns="3" rendered="#{testTreeBean.data != null}">
<p:outputLabel value="someLabel1" for="@next" />
<p:inputText value="#{testTreeBean.data.someText1}" required="true" />
<p:message for="@previous" />
<p:outputLabel value="someLabel2" for="@next" />
<p:inputText value="#{testTreeBean.data.someText2}" required="true" />
<p:message for="@previous" />
</p:panelGrid>
<h:outputText value="please select a node in the left pane"
rendered="#{testTreeBean.data == null}" />
</p:outputPanel>
</p:layoutUnit>
</p:layout>
</h:form>
@javax.faces.bean.ManagedBean
@javax.faces.bean.ViewScoped
public class TestTreeBean implements Serializable
{
private static final long serialVersionUID = 1L;
private TreeNode root;
private TreeNode selected;
// build a dummy tree...
@PostConstruct
public void init()
{
root = new DefaultTreeNode();
for(int i = 0; i < 5; i++)
{
SomeData data = new SomeData("node " + i, String.valueOf(System.currentTimeMillis()));
TreeNode node = new DefaultTreeNode(data, root);
for(int j = 0; j < 5; j++)
{
SomeData subData = new SomeData("subNode " + i + "." + j, String.valueOf(System.currentTimeMillis()));
@SuppressWarnings("unused")
TreeNode subNode = new DefaultTreeNode(subData, node);
}
}
}
// handle selection swap manually
public void onSelect(NodeSelectEvent event)
{
if(selected != null)
{
selected.setSelected(false);
}
selected = event.getTreeNode();
if(selected != null)
{
selected.setSelected(true);
}
}
// shortcut for getting the selected node data
public Object getData()
{
return selected == null ? null : selected.getData();
}
public TreeNode getSelected()
{
return selected;
}
public TreeNode getRoot()
{
return root;
}
}
public class SomeData implements Serializable
{
private String someText1;
private String someText2;
public SomeData()
{
super();
}
public SomeData(String someText1, String someText2)
{
super();
this.someText1 = someText1;
this.someText2 = someText2;
}
public String getSomeText1()
{
return someText1;
}
public void setSomeText1(String someText1)
{
this.someText1 = someText1;
}
public String getSomeText2()
{
return someText2;
}
public void setSomeText2(String someText2)
{
this.someText2 = someText2;
}
}