I just went through the same thing. In struts 2.1 there is a new href attribute
on the tree tag. It points to an action that returns some json using the json
plugin that gives all the info about the node. You should be able to use it in
Struts 2.0.9 as well by using the dew dojo plugin and by using the json plugin.
My code looks like this:
In my jsp I add a reference to the struts dojo tags in the new dojo plugin:
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
...
<s:url var="treeUrl" action="treeLookup" />
<sx:tree href="%{#treeUrl}" />
My action configuration for treeLookup is in a package extending json-default,
to allow me to return json:
<package name="eqd" namespace="/" extends="json-default">
...
<action name="treeLookup"
class="org.tds.eqd.website.actions.usermanager.UserManagerAction">
<result type="json">
<param name="root">options</param>
</result>
</action>
</package>
Note that I made the root of my json the options parameter of my action. The
options parameter gets turned into json automatically and gets sent back to the
client.
The tree will call the treeLookup action passing a parameter called nodeId.
When it's null, you return the root node. Otherwise return the children for the
node passed.
public class UserManagerAction {
private String nodeId;
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public Collection<TreeBean> getOptions() {
...
}
}
public class TreeBean {
private final boolean hasChildren;
private final String id;
private final String label;
public TreeBean(boolean hasChildren, String id, String label) {
this.hasChildren = hasChildren;
this.id = id;
this.label = label;
}
public boolean isHasChildren() {
return hasChildren;
}
public String getId() {
return id;
}
public String getLabel() {
return label;
}
}
In the getOptions method you send back a collection of beans that have a label,
an id, and whether or not they have children.
You could look up the node in hibernate using the id, then iterate over that
nodes children and create beans for them.
The final format for the json should look something like:
[
{
label: "text of node1",
id: "id1",
hasChildren: true
},
{
label: "text of node2",
id: "id2",
hasChildren: false
}
]
See https://issues.apache.org/struts/browse/WW-1910 for more info. Remember to
put the json and dojo plugins in you lib directory.
-----Original Message-----
From: Cosmin Stanciu [mailto:[EMAIL PROTECTED]
Sent: October 18, 2007 1:50 PM
To: Struts Users Mailing List
Subject: Re: <s:tree> and Hibernate
Yes! My Jsp file looks exactly like the one provided into the "Example
Applications" (struts-2.0.9-apps.zip).
<script>
function treeNodeSelected(nodeId) {
dojo.io.bind({
url: "<s:url value='/tags/ui/ajax/dynamicTreeSelectAction.action'
/>?nodeId="+nodeId,
load: function(type, data, evt) {
var displayDiv = dojo.byId("displayId");
displayDiv.innerHTML = data;
},
mimeType: "text/html"
});
};
dojo.event.topic.subscribe("treeSelected", this, "treeNodeSelected");
</script>
<s:tree
theme="ajax"
rootNode="%{treeRootNode}"
childCollectionProperty="children"
nodeIdProperty="id"
nodeTitleProperty="name"
treeSelectedTopic="treeSelected">
</s:tree>
The tree works fine with the data provided in the example application but I
can't make it work with the data received from my Hibernate layer. So I just
want to know I should write the "ShowDynamicTreeAction.java".
Thank you!
Cosmin
----- Original Message ----
From: Dave Newton <[EMAIL PROTECTED]>
To: Struts Users Mailing List <[email protected]>
Sent: Thursday, October 18, 2007 7:27:32 PM
Subject: Re: <s:tree> and Hibernate
--- Cosmin Stanciu <[EMAIL PROTECTED]> wrote:
> Action tab the funtionality is not shown. :(
Are you filling tree data via Ajax (which the example
on the wiki isn't)?
d.
---------------------------------------
Hi!
I have been trying for a week now to populate one <s:tree> with the data from a
database table but with no luck . I would really apreciate some guidance in how
to write the ShowDynamicTreeAction class that sends the date to the tree.
I'm using Hibernate DAO to receive the data from my database table and it looks
like this.
Category: id, name, parent_category_id (from a a recursive 1:n relation to the
same table using the id).
The Hibernate generated object (Category.java) has the fallowing fields:
private Integer id;
private Category category;
private String name;
private Set categories = new HashSet(0);
In the CategoryDAO class I have all the methods needed to retreive data from
the table:
- Category findById(int Id), List findById(int Id), List findParents() and I
can add any needed method...
My struts.xml has a reference to the class showDynamicTreeAction:
<action name="showDynamicTreeAction"
class="struts2.ShowDynamicTreeAction">
<result>/jsp/CategoryList.jsp</result>
</action>
I'm sure that helping solving this issue would be of much interest for all
those that intend to use s:tree in their application, as there no example or
help regarding this matter on the internet.
Thanks a lot!
Cosmin
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
********************
NOTICE OF CONFIDENTIALITY
This communication including any information transmitted with it is
intended only for the use of the addressees and is confidential.
If you are not an intended recipient or responsible for delivering
the message to an intended recipient, any review, disclosure,
conversion to hard copy, dissemination, reproduction or other use
of any part of this communication is strictly prohibited, as is the
taking or omitting of any action in reliance upon this communication.
If you receive this communication in error or without authorization
please notify us immediately by return e-mail or otherwise and
permanently delete the entire communication from any computer,
disk drive, or other storage medium.
If the above disclaimer is not properly readable, it can be found at
www.td.com/legal
AVERTISSEMENT DE CONFIDENTIALITE
Ce courriel, ainsi que tout renseignement ci-inclus, destiné uniquement
aux destinataires susmentionnés, est confidentiel. Si vous
nêtes pas le destinataire prévu ou un agent responsable de la
livraison de ce courriel, tout examen, divulgation, copie, impression,
reproduction, distribution, ou autre utilisation dune partie de ce
courriel est strictement interdit de même que toute intervention ou
abstraction à cet égard. Si vous avez reçu ce message par erreur ou
sans autorisation, veuillez en aviser immédiatement lexpéditeur par
retour de courriel ou par un autre moyen et supprimer immédiatement
cette communication entière de tout système électronique.
Si l'avis de non-responsabilité ci-dessus n'est pas lisible, vous
pouvez le consulter à www.td.com/francais/legale