0

我想处理这个表单(valueChangeListener 在实际情况下无效)。

这是后面的bean:

@Component
@Scope("request")
public class TestBean extends PrivateBaseBean implements Serializable {
protected static final Logger logger = Utils.loggerForThisClass();
private TreeNode root;

@PostConstruct
public void init() {
    root = new DefaultTreeNode("root", null);

    TreeNode child1 = new DefaultTreeNode(new Element("Total"), root);

    new DefaultTreeNode(new Element("Office"), child1);

}

public void saveAction() {

    StringBuilder textToShowInMessage = new StringBuilder();
    for (TreeNode children : root.getChildren()) {
        logger.debug(((Element) children.getData()).getName() + "->"
                + ((Element) children.getData()).getValue());
        for (TreeNode leaf : children.getChildren()) {
            logger.debug(((Element) leaf.getData()).getName() + "->"
                    + ((Element) leaf.getData()).getValue());
        }
    }

}

public TreeNode getRoot() {
    return root;
}

public void setRoot(TreeNode root) {
    this.root = root;
}

元件型号:

public class Element {

private String name;
private String value;

public Element(String name) {
    super();
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

最后是观点:

<h:form>

    <p:treeTable value="#{testBean.root}" var="element">

        <p:column>
            <f:facet name="header">
                        Name
                    </f:facet>
                    #{element.name}
                </p:column>

        <p:column>
            <f:facet name="header">
                        Value
                    </f:facet>
            <h:inputText value="#{element.value}" />
        </p:column>
    </p:treeTable>
    <p:commandButton value="#{msg.save}" action="#{testBean.saveAction}"
        process="@all" icon="ui-icon-disk" update="@form" />
</h:form>

我也想处理 TreeNode 中的所有表单值。但是Backing bean中只接收第一个childresn的第一个值。

这是输入:

输入表格

这是输出:

DEBUG: com.smf.web.jsf.bean.TestBean - Total->1231231231
DEBUG: com.smf.web.jsf.bean.TestBean - Office->null

我不知道如何处理所有树值...

4

1 回答 1

0

一种解决方案是:

1.让我们用树和元素对象制作会话对象数组:

@Component
@Scope("session")
public class SessionObject {
private Object[] root;

public Object[] getRoot() {
    return root;
}

public void setRoot(Object[] root) {
    this.root = root;
}

}

2.注入TestBean:

@Autowired
private SessionObject sessionObject;

3.在 init() 方法中,我们必须在会话中保存所有内容:

Object[] array = new Object[] { root, element, element2 };
    sessionObject.setRoot(array);

4.最后在 saveAction() 中我们可以恢复具有新值的元素:

for (TreeNode children : ((DefaultTreeNode) ((Object[]) sessionObject
            .getRoot())[0]).getChildren()) {
....

会话中的每个元素都是必需的:

背豆:

@Component
@Scope("request")
public class TestBean extends PrivateBaseBean implements Serializable {
@Autowired
private SessionObject sessionObject;
protected static final Logger logger = Utils.loggerForThisClass();
private TreeNode child1;
private Element element;
private Element element2;

@PostConstruct
public void init() {
    sessionObject = new SessionObject();
    element = new Element("Total");
    element2 = new Element("Oficina");

    TreeNode root = new DefaultTreeNode("root", null);

    child1 = new DefaultTreeNode(element, root);

    new DefaultTreeNode(element2, root);
    Object[] array = new Object[] { root, element, element2 };
    sessionObject.setRoot(array);
}

public void saveAction() {
    for (TreeNode children : ((DefaultTreeNode) ((Object[]) sessionObject
            .getRoot())[0]).getChildren()) {
        logger.debug(((Element) children.getData()).getName() + "->"
                + ((Element) children.getData()).getValue());
        for (TreeNode leaf : children.getChildren()) {
            logger.debug(((Element) leaf.getData()).getName() + "->"
                    + ((Element) leaf.getData()).getValue());
        }
    }

}

public TreeNode getRoot() {
    return ((DefaultTreeNode) ((Object[]) sessionObject.getRoot())[0]);
}

public void setRoot(TreeNode root) {
    Object[] array = new Object[] { root, element, element2 };
    sessionObject.setRoot(array);
}
}

现在输出:

DEBUG: com.smf.web.jsf.bean.TestBean - Total->TotalValue
DEBUG: com.smf.web.jsf.bean.TestBean - Oficina->OfficeValue

有关更多详细信息,请在此处询问..

于 2013-05-16T12:22:28.930 回答