当前位置: 代码迷 >> Java Web开发 >> struts2.0+easyUI实现dataGrid,纠结~!
  详细解决方案

struts2.0+easyUI实现dataGrid,纠结~!

热度:404   发布时间:2010-09-17 13:25:02.0
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.w3.org/TR/html4/loose.dtd">
<%@ 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  纠结  

----------------解决方案--------------------------------------------------------
这个问题已经纠结我2个星期了,各位大大,尽量帮下小弟吧!
----------------解决方案--------------------------------------------------------
你可以这么尝试一下,请求
url:'select.do'
完成后,将你的JSON用在一个JSP或者servlet方法中 用 out.println();
输出看看!
----------------解决方案--------------------------------------------------------
能在页面上显示出转换出来的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里面去!

----------------解决方案--------------------------------------------------------
问题解决了,跟大家分享下
是思路出了问题
原来的是想运行jsp页面然后调用到action里面,再从action里面返回到页面,这样的想法在一般的程序中是没问题,关键在与这里需要利用response把json对象write到jsp页面,相当于jsp页面里的内容都是json对象了
正确的死路应该是把json对象返回到jsp页面,在jsp页面获取了以后进行遍历,就是说首先要运行action,就是这里的select.do(http://localhost:8888/select.do) ,然后才会产生json对象,把json对象传递到jsp页面就行了!  
谢谢各位的帮忙,问题解决了!
----------------解决方案--------------------------------------------------------
大哥,还是不懂啊,我跟你遇到我问题差不多啊,就是执行.action的时候就跳转到下载,我点保存下载,然后用记事本打开,发现里边的内容是json格式的内容,
就是不能返回到页面上,我郁闷了   
----------------解决方案--------------------------------------------------------
这是我的QQ,可以加我QQ请教你吗?
13225292
谢谢你了
----------------解决方案--------------------------------------------------------
  相关解决方案