p:treeTable中的h:inputText的值未处理 [英] Values of h:inputText inside p:treeTable are not processed
问题描述
我要处理这种形式(valueChangueListener在实际情况下无效).
I want to process this form (valueChangueListener is not valid in real case).
这是背景豆:
@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的第一个值.
I want to process all form values inside TreeNode also. But only first value of first childresn is recived in Backing bean.
这是意思:
这是输出:
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.让我们使用树和元素对象创建会话对象数组:
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.注入TestBean:
2.Inject in TestBean:
@Autowired
private SessionObject sessionObject;
3.在init()方法中,我们必须将所有内容保存在会话中:
3.In init() method we have to save all in session:
Object[] array = new Object[] { root, element, element2 };
sessionObject.setRoot(array);
4.最后,在saveAction()中,我们可以使用新值恢复元素:
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);
}
}
现在输出:
DEBUG: com.smf.web.jsf.bean.TestBean - Total->TotalValue
DEBUG: com.smf.web.jsf.bean.TestBean - Oficina->OfficeValue
有关更多详细信息,请在这里..
For more details ask here..
这篇关于p:treeTable中的h:inputText的值未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!