您的位置:首页 > Web前端 > JavaScript

json数据传输

2012-03-12 10:50 267 查看
Java代码:

UserAction.java:

protected transient HttpServletResponse response;

 public String queryList(List<OppUser> list)

 {

  this.outJsonData(listTojson(list));

 }

 protected void outJsonData(Object object)

 {

  // 转换成json对象

  JSONArray jsonObject = JSONArray.fromObject(object);

  // 获得转换后的json字符串

  String jsonStr = jsonObject.toString();

  try

  {

   // 将查询结果展示到页面

   this.writeJson(jsonStr, response);

  }

  catch (IOException e)

  {

   log.warn(e, "...");

  }

 }

 protected void writeJson(String jsonStr, HttpServletResponse resp)

   throws IOException

 {

  String fullContentType = "application/json;charset=UTF-8";

  resp.setContentType(fullContentType);

  resp.setHeader("Pragma", "No-cache");

  resp.setHeader("Cache-Control", "no-cache");

  resp.setDateHeader("Expires", 0);

  PrintWriter pw = null;

  try

  {

   pw = resp.getWriter();

   pw.print(jsonStr);

  }

  finally

  {

   if (pw != null)

   {

    pw.close();

    pw.flush();

    resp.flushBuffer();

   }

  }

 }

 public List<OppUserJSON> listTojson(List<OppUser> list)

 {

  List<OppUserJSON> oppUserList = new ArrayList<OppUserJSON>();

  // 账号信息对象

  OppUser oppUser = new OppUser();

  int listSize = list.size();

  if (listSize > 0)

  {

   for (int i = 0; i < listSize; i++)

   {

    OppUserJSON oppUserJSON = new OppUserJSON();

    oppUser = (OppUser) list.get(i);

    oppUserJSON.setOppUser(oppUser);

    oppUserJSON.setUserId(oppUser.getUserId());

    oppUserJSON.setUserName(oppUser.getUserName());

    oppUserJSON.setPageNum(2 + "");

    oppUserJSON.setPageSize(10 + "");

    oppUserList.add(oppUserJSON);

   }

  }

  return oppUserList;

 }

OppUserJSON.java:

private OppUser oppUser;

    private String userId;

    private String userName;

    private String pageNum;

    private String pageSize;

//省略set、get方法...

public String toString()

    {

        String line = System.getProperty("line.separator");

        StringBuffer sb = new StringBuffer("");

        sb.append('{');

        sb.append(line);

        sb.append("oppUser=").append(this.oppUser).append(line);

        sb.append("userId=").append(this.userId).append(line);

        sb.append("userName=").append(this.userName).append(line);

        sb.append("pageNum=").append(this.pageNum).append(line);

        sb.append("pageSize=").append(this.pageSize).append(line);

        sb.append('}');

        return sb.toString();

    }

struts.xml:

<action name="queryList" class="userAction" method="queryList">

  <result name="json"/>

 </action>

js:

function queryList()

       {

           var param = "pageNum="+pageno;

           $.ajax({

                type:"post",

                url:"queryList.action",

                data:param,

                dataType:"json",

                beforeSend: function()

                {

                    $("#accountListTable tr:not(:first)").hide();

                },

                success: function(json)

                {

                    //加载数据

                    loadAccountInfo(json);

                },

                error: function()

                {

                    popDialog.show("提示信息", "查询失败");

                }

            });

       }     

function loadAccountInfo(json)

   { 

       if (json != null && json.length != 0)

       {

           //组织列表数据

           for(var i=0; i<json.length; i++)

           {

            fillAccountTable(json[i]);

           } 

        }

       else

       {

           var tr = $("<tr></tr>");

           var td = $("<td colspan='3'></td>");

           td.addClass("tdInfo");

           td.append("没有找到相关信息");

           tr.append(td);

           toneInfoTable.append(tr);

           $("#pageHtml").hide();

       }   

   }

function fillAccountTable(jsoni)

    { 

 var cloneTr = $("#firstTr:first").clone(true);

 $("#accountListTable").append(cloneTr);

 cloneTr.show();  

 

 cloneTr.find("#userId").text(jsoni.userId);

 cloneTr.find("#userName").text(jsoni.userName); 

 cloneTr.find("#userName").bind("click", function(){

  var param = "id=" + jsoni.userId;

  var url = 'xxx.action?subAction=accountmanager&' + param;

  window.location.href = url; 

 });

    }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  function json url