首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Spring MVC Controller中的数据填充ExtJS树?

问如何使用Spring MVC Controller中的数据填充ExtJS树?
EN

Stack Overflow用户
提问于 2011-04-05 20:56:35
回答 1查看 7K关注 0票数 0

作为ExtJS的新手,我正在尝试将其与Spring MVC Controller结合使用。所以我的结构如下: tree.jsp从SomeController.java获取JSON数据。我用瓦片2把它们连接起来。

tree.jsp

代码语言:javascript
复制
<script type="text/javascript">

if (Ext.BLANK_IMAGE_URL.substr(0,5) != 'data'){
    Ext.BLANK_IMAGE_URL = '/ext-3.3.1/resources/images/default/s.gif';
}

Ext.onReady(function(){

    var proxy = new Ext.data.HttpProxy({
        api: {
            read: 'jsonTree'
        }    
    });

    var treeLoader = new Ext.tree.TreeLoader({
        proxy: proxy
    });

    // shorthand
    var Tree = Ext.tree;

    var tree = new Tree.TreePanel({
        el:'tree-div', //we need to have a div in our html named this
        title: "Data Sources",
        useArrows:true,
        autoScroll:true,
        animate:true,
        enableDD:true,
        containerScroll: true,
        rootVisible: false,
        loader: treeLoader,

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

    // render the tree
    tree.render();
});

​

SomeController.java

代码语言:javascript
复制
@Controller
@RequestMapping("/some")
public class SomeController {

private Logger log = LoggerFactory.getLogger(CountryReferenceBookController.class);
private static final String LIST_VIEW_KEY = "redirect:list.html";
@Autowired
private RestTemplate restTemplate;

@RequestMapping(value = "/jsonTree/{languageId:\\d+}")
public @ResponseBody Map<String, ? extends Object> getJsonTreeInfo(@PathVariable("languageId") Long languageId) {

    List treeList = new ArrayList<Map<String, Object>>();

    List<ReferenceBookTitleImpl> list = new ArrayList<ReferenceBookTitleImpl>();
    list = restTemplate.getForObject("http://localhost:8084/xcollector/restService/reference/list", list.getClass());


    for (ReferenceBookTitleImpl title : list) {
        String description = restTemplate.getForObject("http://localhost:8084/xcollector/restService/reference/title/" + title.getId() + "/content/" + languageId, String.class);

        //create a child node that is a leaf
        Map child = new HashMap();
        child.put("id", title.getId());
        child.put("text", description);
        child.put("checked", Boolean.FALSE);
        child.put("leaf", Boolean.TRUE);

        treeList.add(child);
    }

    //the spring mvc framework takes a hashmap as the model..... :| so in order to get the json array to the View, we need to put it in a HashMap
    Map modelMap = new HashMap();
    modelMap.put("JSON_OBJECT", treeList);

    log.info("jsonArray: " + treeList.toString());
    return modelMap;
}

}

当我向控制器发送GET响应时,我得到一个包含json数组的文件,而不是填充的树。我的代码出了什么问题?

提前谢谢你,L。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-08 14:56:06

因为没有人问我,我决定自己回答我的问题。主要问题是对AJAX调用的错误理解。

如果你想从Spring MVC Controller中填充一个树形面板,你必须实现两个方法:第一个返回树形面板所在的视图,第二个返回填充树形面板的数据。

让我们来看一下这个例子:

首先,我在项目中使用了Tiles 2。因此,在一个带有磁贴的文件(templates.xml)中,我添加了以下磁贴:

负责处理url http://<ip:port>/<web-app>/tree上的请求的控制器有两种方法:

此方法返回jsp视图:

代码语言:javascript
复制
@RequestMapping(value = "/tree")
public ModelAndView getTreeView() {
    return new ModelAndView("/tree");
}

Next方法返回json数据:

代码语言:javascript
复制
@RequestMapping(value = "/tree/data", method=RequestMethod.POST)
public @ResponseBody List<Map<String, Object>>  getJsonTreeInfo() {

    List treeList = new ArrayList<Map<String, Object>>();

    List<FooImpl> list = new ArrayList<FooImpl>();
    list = restTemplate.getForObject("http://localhost:8084/<web-app>/restService/list", list.getClass());


    for (FooImpl foo : list) {
        String description = restTemplate.getForObject("http://localhost:8084/<web-app>/restService/title/" + foo.getId() + "/content/11", String.class);

        //create a child node that is a leaf
        Map child = new HashMap();
        child.put("id", foo.getId());
        child.put("text", description);
        child.put("leaf", Boolean.TRUE);

        treeList.add(child);
    }
    return treeList;
}

可能不清楚这个列表是如何变成json数组的。我有一个消息转换器,它是做所有的转换工作。

JSP端:

代码语言:javascript
复制
<script type="text/javascript">
    if (Ext.BLANK_IMAGE_URL.substr(0,5) != 'data'){
        Ext.BLANK_IMAGE_URL = '/ext-3.3.1/resources/images/default/s.gif';
    }

    // application main entry point
    Ext.onReady(function() { 

    // shorthand
    var Tree = Ext.tree;

    var tree = new Tree.TreePanel({
        useArrows: true,
        autoScroll: true,
        animate: true,
        enableDD: true,
        containerScroll: true,
        border: false,
        // auto create TreeLoader
        dataUrl: 'tree/data',

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

    // render the tree
    tree.render('tree-div');
    tree.getRootNode().expand();
});

</script>

<div id="tree-div"></div>

这一行dataUrl: 'tree/data'向第二个控制器的方法发送POST请求。它返回json数组。最后,树中填充了数据:)每个人都很高兴:)

我希望它能帮助一些人,因为我花了很多时间来解决这个问题。

享受您的工作:)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5552173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档