Struts + JSON + prototype

Posted on 2008-01-09 19:13 Martin Yuan 阅读(176) 评论(0)  编辑  收藏

json-lib-1.1-jdk15.jar并不包含其所依赖的类库。现将其依赖库补全。

JSON-LIB依赖库如下
EZMorph 下载地址http://ezmorph.sourceforge.net/
Commons Collections http://jakarta.apache.org/commons/collections/
Commons Lang
http://jakarta.apache.org/commons/lang/


JsonAction

-----------------------------------------------------------------------------------------------------

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class JsonAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    Log log = LogFactory.getLog(JsonAction.class);
    try {
     JSONArray itemList = new JSONArray();
     for (int i = 0; i < 5; i++) {
      JSONObject ci = new JSONObject();
      ci.put("menuid", "1111");
      ci.put("menuName", "wwwwwwwww");
      ci.put("url", "http://www.baidu.com/");
      itemList.put(ci);
     }
     JSONObject json = new JSONObject();
     json.put("itemList", itemList);
     response.setHeader("X-JSON", json.toString());
     response.setCharacterEncoding("UTF-8");
     response.setHeader("Cache-Control", "no-cache");// 清楚缓存
     log.info(json.toString());
     response.getWriter().print(json.toString());
    } catch (Exception e) {
     HashMap hm = new HashMap();
     hm.put("executeError",
       "Couldn’t find the Full Name because an error occured.");
     log.error("Couldn’t find the Full Name because an error occured.");
     JSONObject json = JSONObject.fromObject(hm);
     response.setHeader("X-JSON", json.toString());
     response.setCharacterEncoding("UTF-8");
    }
    return null;
}

}

test.jsp

-----------------------------------------------------------------------------------------------------

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
      <title>My JSP 'test.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
    </head>
  
    <script type="text/javascript" src="prototype.js"></script>
    <script language="javascript" type="text/javascript">
var dd={lookup:function ()
                 {
                      var url = 'json.do';
                      var pars = '';
                      var myAjax = new Ajax.Request(url,{
                          method:'get',
                          encoding:'UTF-8',
                          parameters:pars,
                          onComplete:function(json){
                              var JSONobj=eval('('+ json.responseText +')');
                              var html="" ;
                              var menuid="";
                              for (var i=0; i< JSONobj.itemList.length; i++){
                                  var item = JSONobj.itemList[i];
                                  html += "<li>" + item.menuid+ item.menuName +item.url +"</li>";
                              }
                              $("item").innerHTML=html;
                          }
                      });
               
                 }
}
</script>
    <body>
      <input type="button" name="button" id="button" onclick="dd.lookup();"value="点击测试">
      <div id="item"></div>
    </body>
</html>

-------------------------------------------------------------------------------------

Ext实现

grid.js

-------------------------------------------------------------------------------------

Ext.onReady(function(){

     // create the Data Store
     var ds = new Ext.data.Store({
         // load using HTTP
         proxy: new Ext.data.HttpProxy({url: '../json.do'}),//Url很关键,我就是因为没配好这个,耽误了一大堆时间!

         // the return will be XML, so lets set up a reader
         reader: new Ext.data.JsonReader({
                // records will have an "Item" tag
                root:"BOOKARRAY"//这里包括下面的,改为对应的
            }, [
                // set up the fields mapping into the xml doc
                // The first needs mapping, the others are very basic
               'KEYWORDS','ISBN','Title', 'Manufacturer', 'ProductGroup'
            ])
     });

     var cm = new Ext.grid.ColumnModel([
      {header: "KEYWORDS", dataIndex: 'KEYWORDS'},
      {header: "ISBN", dataIndex: 'ISBN'},
   {header: "Title",   dataIndex: 'Title'},
   {header: "Manufacturer",   dataIndex: 'Manufacturer'},
   {header: "Product Group", dataIndex: 'ProductGroup'}
]);
     cm.defaultSortable = true;

     // create the grid
     var grid = new Ext.grid.Grid('example-grid', {
         ds: ds,
         cm: cm
     });
     grid.render();

     ds.load();
});



I just want to live while i'm alive .


只有注册用户登录后才能发表评论。


网站导航: