| This article describes how to code an EXT JS tree control to display directories and files using Java, Spring, and JSON. |
This kicked my ass for more than an hour, so I thought I better post the solution to save someone from the same heart-ache. Unfortunately, I don't have any time left to actually explain this, so you're going to just have to turn your brain on and read between the lines.
The EXT JS Code
The Spring Controller
Here's the controller that will be fired when the dataUrl defined above is requested:
Note that the tree will always send the node id as a request parameter when it makes its async request, so - we are capturing that param. I've programmed the param to be the actual file name so that when we keep clicking on directories in the tree, we'll just call the same controller and keep going deeper and deeper.
There's an annotation that defines the dataUrl:
@RequestMapping(CNST.MY_DATA_URL)
CNST.MY_DATA_URL is a constant in my case. In your case it can be a String equal to the dataUrl you're EXT JS tree is going to call. In other words:
CNST.MY_DATA_URL = '/path/that/initiates/your/controller/goes/here';
Creating the JSON
Now here's how the JSON is created by the FileUtils class:
This produces JSON output that looks like this:
The JSP
I'm using Tiles, so I don't have a bunch of markup in the JSP that delivers the JSON to the requesting tree control. It's dead simple:
The User Interface
In the end, it looks like this:

Each directory node will load asynchronously.
Sorry I didn't spell everything out, but hopefully something in here might make at least one struggling developer's day a little brighter.
Happy coding!
7 Comments
comments.show.hideOct 04, 2010
Anonymous
pleage attatch war file
Oct 05, 2010
Michael Minardi
Cody, just curious, do you go to any other external wiki's, blogs or other channels when you run into a technical problem you are trying to solve. You are always diligent about recording these solutions and building our assets, was just wondering if you try to leverage the external network and sites for new problems.
Oct 06, 2010
Cody Burleson
Absolutely! The first thing I do when looking to solve a new problem is "Google" it. Typically, someone in the community has figured it out and has also been kind enough to make a post. I only post about things that do not turn up in search results or things with posted solutions that are, in my opinion, unsatisfactory. Certainly, I have been saved many times by enthusiastic developers who post solutions, so I try to make it a point to give back when I can. It's my way of being involved in 'my community'.
Oct 06, 2010
Cody Burleson
Sorry - I'm not going to prepare a WAR file just for this. And I will not attach the WAR from which these sources are a part, because that is a proprietary app. If you're stuck, explain your problem, and we'll try to help.
Oct 16, 2010
Anonymous
Poxa Cara valeu mesmo. Procurei tanto um exemplo assim. Você me ajudou bastante. Muito obrigado. Fique na paz...
Oct 23, 2010
Anonymous
I have "cannot be resolved " error when use your LocalModelsTreeJsonController code in my project.
Whether AbstractBaseModelingControll.class and modelService are located in special jar file ?
Jan 11, 2011
Anonymous
@Cody Burleson: Sir i am doing the same thing as you did in this post. I have a spring controller which have multiple custom methods each having its own mapped url. My tree loader is not able to call the specified url. i have a url pattern like /example/loadtree.action where .action is a servlet url pattern defined in web.xml. Please help if you have time
Thanks
Imran Ullah
Software Engineer