Reputation: 23
I am currently trying to get familiar with Java Faces (or the hole Java EE universe in general) and wrote a small example with the PrimeFaces tree and dialog components. Basically I want to select a entry of the displayed tree and show the details of the underlying data object in a dialog.
The tree gets displayed, I can select an entry, the onSelect handler in my bean gets executed via a Ajax call and the dialog shows up. Unfortunately the inputText components, in the dialog, are empty although the connected object on the server side is valid and it's attributes are filled with the selected tree entry, which I have verified via a simple System.out.println
.
I would have expected that the roudtrip to the server, via the ajax call, sends the changed values (on the server side) back to the client, which apparently is not the case according to the network tab in the developer tools of the browser.
TestClass.java:
public class TestClass {
private String name;
private String otherAttribute;
public TestClass(String name, String otherAttribute) {
this.name = name;
this.otherAttribute = otherAttribute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOtherAttribute() {
return otherAttribute;
}
public void setOtherAttribute(String otherAttribute) {
this.otherAttribute = otherAttribute;
}
public String toString() {
return new String("TestClass(name=" + this.name + ", otherAttribute=" + this.otherAttribute + ")");
}
}
TestService.java:
@Named
@ApplicationScoped
public class TestService {
public TreeNode<TestClass> createTestTree() {
TreeNode<TestClass> root = new DefaultTreeNode<TestClass>(new TestClass("root", null), null);
TreeNode<TestClass> entry1 = new DefaultTreeNode<TestClass>(new TestClass("entry1", "attribute of entry1"), root);
TreeNode<TestClass> entry2 = new DefaultTreeNode<TestClass>(new TestClass("entry2", "attribute of entry2"), root);
TreeNode<TestClass> entry3 = new DefaultTreeNode<TestClass>(new TestClass("entry3", "attribute of entry3"), root);
TreeNode<TestClass> subentry11 = new DefaultTreeNode<TestClass>(new TestClass("subentry11", "subentry 1 attribute of entry1"), entry1);
TreeNode<TestClass> subentry12 = new DefaultTreeNode<TestClass>(new TestClass("subentry12", "subentry 2 attribute of entry1"), entry1);
TreeNode<TestClass> subentry13 = new DefaultTreeNode<TestClass>(new TestClass("subentry13", "subentry 3 attribute of entry1"), entry1);
TreeNode<TestClass> subentry21 = new DefaultTreeNode<TestClass>(new TestClass("subentry21", "subentry 1 attribute of entry2"), entry2);
TreeNode<TestClass> subentry22 = new DefaultTreeNode<TestClass>(new TestClass("subentry22", "subentry 2 attribute of entry2"), entry2);
TreeNode<TestClass> subentry31 = new DefaultTreeNode<TestClass>(new TestClass("subentry31", "subentry 1 attribute of entry3"), entry3);
return root;
}
}
TestBean.java:
@Named("testBean")
@ViewScoped
public class TestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4448740441931801702L;
@Inject
TestService treeService;
private TreeNode<TestClass> root;
private TestClass selectedNode;
@PostConstruct
private void init() {
this.root = this.treeService.createTestTree();
}
public void onTreeNodeSelect(NodeSelectEvent event) {
System.out.println("NodeSelectEvent: " + event);
this.selectedNode = (TestClass)event.getTreeNode().getData();
System.out.println("Selected Node" + this.selectedNode);
System.out.println("Showing dialog");
PrimeFaces.current().executeScript("PF('treeNodeEditor').show()");
}
public TreeNode<TestClass> getRoot() {
return root;
}
public void setRoot(TreeNode<TestClass> root) {
this.root = root;
}
public TestClass getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TestClass selectedNode) {
this.selectedNode = selectedNode;
}
}
index.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Tree test</title>
</h:head>
<h:body>
<div class="grid">
<div class="col-12">
<h:form>
<p:tree value="#{testBean.root}" var="node" selectionMode="single">
<p:ajax event="select" listener="#{testBean.onTreeNodeSelect}"></p:ajax>
<p:treeNode>
<h:outputText value="#{node.name}"></h:outputText>
</p:treeNode>
</p:tree>
</h:form>
</div>
</div>
<p:dialog widgetVar="treeNodeEditor">
<div class="grid">
<div class="col-6">
<p:outputLabel for="@next" value="Name"></p:outputLabel>
<p:inputText style="width: 100%" value="#{testBean.selectedNode.name}"></p:inputText>
</div>
<div class="col-6">
<p:outputLabel for="@next" value="Attribute"></p:outputLabel>
<p:inputText style="width: 100%" value="#{testBean.selectedNode.otherAttribute}"></p:inputText>
</div>
</div>
</p:dialog>
</h:body>
</html>
Upvotes: 0
Views: 40