Values of h:inputText inside p:treeTable are not processed

只谈情不闲聊 提交于 2019-12-25 03:11:08

问题


I want to process this form (valueChangueListener is not valid in real case).

This is the back 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;
}

Element model:

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;
}

Finally the view:

<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>

I want to process all form values inside TreeNode also. But only first value of first childresn is recived in Backing bean.

This is the intput:

This is the output:

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

I don't know how to process all tree values...


回答1:


One solution is this:

1.Let's make session object array with tree and elements objects:

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

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

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

}

2.Inject in TestBean:

@Autowired
private SessionObject sessionObject;

3.In init() method we have to save all in session:

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

4.Finally in saveAction() we can recover the elements with new values:

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

Is necessary every element in session:

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);
}
}

Now the output:

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

For more details ask here..



来源:https://stackoverflow.com/questions/16564703/values-of-hinputtext-inside-ptreetable-are-not-processed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!