标题:struts2.0+easyUI实现dataGrid,纠结~!
只看楼主
chenyu329134
Rank: 2
等 级:论坛游民
帖 子:36
专家分:21
注 册:2010-9-17
结帖率:100%
已结贴  问题点数:0 回复次数:7 
struts2.0+easyUI实现dataGrid,纠结~!
先跟大家说一下我遇到的问题:
我用的SSH框架,用的注解,就是想把获取到的list转换成easyUI的Json,然后发送到jsp页面,生成列表,目前我能把list转换成Json对象,而且格式符合easyUI的格式规范,就是不能把Json放到jsp的列表中去。运行jsp页面的时候只能显示一个列表框,没有任何信息!
下面是我的代码:
Action 类:
    /**
     * 获取角色列表
     *
     * @author chenj 2010-9-14
     */
    @Action(value = "select", results = { @Result(name = "error", location = "/role/saveError.jsp", type = "redirect"),
            @Result(name = "success", location = "/role/saveSuccess.jsp", type = "redirect") })
    public String getAllUmRole() {
        String result = "";
        String Json = "";
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            JSONArray a = new JSONArray();
            PrintWriter out = response.getWriter();
            List<UmRole> list = entityManager.getAll(UmRole.class);
            for (UmRole obj : list) {
                a.add(ModelHelper.getInstance().toJSON(obj, true));
            }
            Json = a.toJSON(false);
            out.print(Json.toString());
            System.out.println(Json.toString());
            result = com.opensymphony.xwork2.Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            result = com.opensymphony.xwork2.Action.ERROR;
        }
        return result;
    }
备注:有些类跟方法是自己封装的

JSP页面代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>jQuery EasyUI</title>
    <link rel="stylesheet" type="text/css" href="../jqueryeasyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../jqueryeasyui/themes/icon.css">
    <script type="text/javascript" src="../jqueryeasyui/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="../jqueryeasyui/jquery.easyui.min.js"></script>
    <script>
    $(function(){
        $('#tt').datagrid({
            title:'Load Data',
            iconCls:'icon-save',
            width:600,
            height:250,
            type:'Json',
            url:'select.do',
            frozenColumns:[[
                {field:'id',checkbox:true}
               
            ]],
            columns:[[
                {field:'id',title:'id',width:80},
                {field:'name',title:'name',width:80},
                {field:'des',title:'des',width:80,align:'right'}
            ]],
            pagination:true,
            
             toolbar:[{
                    id:'btnadd',
                    text:'新增',
                    iconCls:'icon-add',
                    handler:function(){
                        $('#btnsave').linkbutton('enable');
                        openNewPage('openCreatePage.do');
                    }
                },{
                    id:'btncut',
                    text:'修改',
                    iconCls:'icon-cut',
                    handler:function(){
                        $('#btnsave').linkbutton('enable');
                        updatePoJo('openUpdatePage.do');
                    }
                },{
                    id:'btnadd',
                    text:'删除',
                    iconCls:'icon-cancel',
                    handler:function(){
                        $('#btnsave').linkbutton('enable');
                        deletePoJo('delete.do');
                    }
                }]
        });
    });
    function deletePoJo(action){
            var ids=document.getElementsByName("id");
            var id = "";
            var num = 0;
            for(var i=0;i<ids.length;i++){
                 if(ids[i].checked){
                     id += ids[i].value+",";
                     num++;
               }
            }
            if(num<=0){
                alert('请选择一条记录后点击');
            }
            else{
                window.location=action+'?id='+id;
               }
        }
         
          function updatePoJo(action){
              var ids = document.getElementsByName("id");
              var id = "";
              var num = 0;
              for(var i = 0;i<ids.length;i++){
                  if(ids[i].checked){
                      num++;
                      id=ids[i];
                  }
              }
              if(num>1){
                  alert("每次只能修改一条记录");
              }
              else if(num<1){
                  alert("请选择一条记录");
              }
              else if(num==1){
                  window.location=action+'?id='+id.value;
              }
          }
         
          function openNewPage(action){
              window.location=action;
          }
    </script>
</head>
<body>
    <table id="tt"></table>
</body>
</html>

运行页面后控制台打印出来的Json:
[{name:"13",des:"13",id:"402880d12b0f715d012b0f71b9d50001"},{name:"123",des:"123",id:"402880d12b0f75ad012b0f75df2c0001"},{name:"345",des:"345",id:"402880d12b0f75ad012b0f77ada30002"},{name:"123",des:"123",id:"402880d12b1db77a012b1db86d9d0001"}]

请教各位高手,如何才能把Json输出到JSP页面并显示出来
搜索更多相关主题的帖子: dataGrid easyUI 纠结 
2010-09-17 13:25
chenyu329134
Rank: 2
等 级:论坛游民
帖 子:36
专家分:21
注 册:2010-9-17
得分:0 
这个问题已经纠结我2个星期了,各位大大,尽量帮下小弟吧!
2010-09-17 14:00
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
得分:7 
你可以这么尝试一下,请求
url:'select.do'
完成后,将你的JSON用在一个JSP或者servlet方法中 用 out.println();
输出看看!
2010-09-17 16:20
chenyu329134
Rank: 2
等 级:论坛游民
帖 子:36
专家分:21
注 册:2010-9-17
得分:0 
能在页面上显示出转换出来的json字符串:[{name:"13",des:"13",id:"402880d12b0f715d012b0f71b9d50001"},{name:"123",des:"123",id:"402880d12b0f75ad012b0f75df2c0001"},{name:"345",des:"345",id:"402880d12b0f75ad012b0f77ada30002"},{name:"123",des:"123",id:"402880d12b1db77a012b1db86d9d0001"}],但还是放不到dataGrid里面去!
2010-09-20 14:15
chenyu329134
Rank: 2
等 级:论坛游民
帖 子:36
专家分:21
注 册:2010-9-17
得分:0 
问题解决了,跟大家分享下
是思路出了问题
原来的是想运行jsp页面然后调用到action里面,再从action里面返回到页面,这样的想法在一般的程序中是没问题,关键在与这里需要利用response把json对象write到jsp页面,相当于jsp页面里的内容都是json对象了
正确的死路应该是把json对象返回到jsp页面,在jsp页面获取了以后进行遍历,就是说首先要运行action,就是这里的select.do(http://localhost:8888/select.do) ,然后才会产生json对象,把json对象传递到jsp页面就行了!  
谢谢各位的帮忙,问题解决了!
2010-09-25 17:54
ton_ch
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-6-11
得分:0 
大哥,还是不懂啊,我跟你遇到我问题差不多啊,就是执行.action的时候就跳转到下载,我点保存下载,然后用记事本打开,发现里边的内容是json格式的内容,
就是不能返回到页面上,我郁闷了   
2011-06-11 17:33
ton_ch
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-6-11
得分:0 
这是我的QQ,可以加我QQ请教你吗?
13225292
谢谢你了
2011-06-11 17:34
Hanson_lee
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-5-12
得分:0 
天哪,这么久的帖子啦。但是我现在才遇到的和你一样的问题,很纠结。以上的方法我也没有理解到。请问可以加qq联系吗?501769678,多谢啊,很急。
2014-05-12 15:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-320699-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.226930 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved