PDA

View Full Version : populating tree from servlet json



mckernn
8 Dec 2009, 5:18 AM
Hi, I am new to extjs (and java), but have managed to get a few working examples using servlets to provide xml for grids and combo's.
However, i am trying to generate json to fill a tree using a similar approach.
My servlet builds the json into a string buffer and posts it to the browser. This works fine for XML, but doesn't appear to work the same for json. I have tried setting the mime type to "aplication/json", "text/x-json" and get different results each time. i.e One will force FF to prompt for down load, and the other writes it to the screen.


sb.append("\n[ ");
sb.append(fileJSON.getResult());
sb.append("\n]");
response.setContentType("text/json");
//response.setContentType("application/json");
//response.setContentType("text/x-json");
response.getWriter().write(sb.toString());
System.out.println(sb);This is the the json output

[
{text: 'testdir', path: 'c:\testdir', children: [
{text: 'file1.txt', path: 'c:\testdir', type: 'Text Document', leaf: true },
{text: 'test.jpeg', path: 'c:\testdir', type: 'JPEG File', leaf: true },
{text: 'test.mpeg', path: 'c:\testdir', type: 'MPEG File', leaf: true }
]}
]

I am trying to implement a basic example of a tree from the the examples (below)
and have tried using dataurl: and a proxy(which works fine for the xml)




Ext.onReady(function(){
// shorthand
// tree structure for the pre-defined genealogies

var Tree = Ext.tree;

var tree = new Tree.TreePanel({
useArrows: true,
autoScroll: true,
animate: true,
enableDD: true,
containerScroll: true,
border: false,
dataUrl: 'http://localhost:8080/ses-author/servlet/getdirJSON',
//proxy: new Ext.data.HttpProxy({url: 'servlet/getdirJSON'}),

root: {
nodeType: 'async',
text: 'Ext JS',
draggable: false,
id: 'src'
}
});

// render the tree
tree.render('tree-div');
tree.getRootNode().expand();
});The debugger in FF shows that its is getting something from the servlet






POST http://localhost:8080/ses-author/servlet/getdirJSON
200 OK
chrome://firebug/content/blank.gif 106msext-base.js (line 7)ParamsHeadersPostPutResponseCacheHTML
Response Headers
Request Headers



[
{text: 'testdir', path: 'c:\testdir', children:
[{text: 'file1.txt', path: 'c:\testdir', type: 'Text Document', leaf: true },
{text: 'test.jpeg', path: 'c:\testdir', type: 'JPEG File', leaf: true },
{text: test.mpeg', path: 'c:\testdir', type: 'MPEG File', leaf: true }
]}

]
Can anyone tell me where I'm going wrong.
Thanks

Franzen
17 Dec 2009, 8:42 AM
Did you resolve this one? I'm having similar troubles myself...