不处理h:inputText在p:treeTable中的值

时间:2013-05-15 12:12:27

标签: java jsf primefaces

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

这是后豆:

@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中处理所有表单值。但是只有第一个childresn的第一个值在Backing bean中被收回。

这是输入:

Input form

这是输出:

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

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

1 个答案:

答案 0 :(得分: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()) {
....

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

BackBean:

@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

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