prototype+json+struts2創建簡單的ajax應用

顯示頁面
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.   <head>
  5.     <title>test</title>
  6.     <meta http-equiv="pragma" content="no-cache">
  7.     <meta http-equiv="cache-control" content="no-cache">
  8.     <meta http-equiv="expires" content="0">    
  9.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  10.     <meta http-equiv="description" content="This is my page">
  11.     <!--
  12.     <link rel="stylesheet" type="text/css" href="styles.css">
  13.     -->
  14.   </head>
  15.     <script type="text/javascript" src="js/common/prototype.js"></script>
  16.     <script language="javascript" type="text/javascript">
  17.         function getJson()
  18.         {
  19.              var url = 'JSONExample.action';
  20.              var pars = '';
  21.              var myAjax = new Ajax.Request(url,{
  22.                  method:'get',
  23.                  encoding:'UTF-8',
  24.                  parameters:pars,
  25.                  onComplete:function(json){
  26.                      var JSONobj=eval('('+ json.responseText +')');
  27.                      var html="" ;
  28.                      var menuid="";
  29.                      
  30.                      html += "<li>" + JSONobj.newName +"</li>";
  31.                      
  32.                      var ints = JSONobj.ints; 
  33.                      for (var i=0; i< ints.length; i++){
  34.                         html += "<li>" + ints[i] +"</li>";
  35.                      }
  36.                      
  37.                      var map = JSONobj.map; 
  38.                      for(array in map){
  39.                         html += "<li>" + array + ":" + map[array] +"</li>";
  40.                      }
  41.                      
  42.                      $("item").innerHTML=html;
  43.                  }
  44.              });
  45.       
  46.         }
  47. </script>
  48.     <body>
  49.       <input type="button" name="button" id="button" onclick="getJson()"value="點擊測試">
  50.       <div id="item"></div>
  51.     </body>
  52. </html>

struts.xml配置文件
  1.     <package name="example"  extends="json-default">  
  2.       <action name="JSONExample" class="jSONExample" method="executeJson">  
  3.          <result type="json"/>  
  4.        </action>  
  5.     </package>   
這裏使用到了json struts2結合的插件,請自己去下載那個jar包


action
  1. package action;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import com.googlecode.jsonplugin.annotations.JSON;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. /**
  7.  *  @author 王盛武 <a href="[email protected]"/> 
  8.  *  @date Aug 1, 200812:10:35 PM
  9.  */
  10. public class JSONExample extends ActionSupport {
  11.     /**
  12.      * 
  13.      */
  14.     private static final long serialVersionUID = 4588482034890372410L;
  15.     // 封裝請求參數的三個屬性
  16.      //format=  例如"yyyy-MM-dd'T'HH:mm:ss"。 
  17.     private String field1;
  18.     private transient String field2;
  19.     
  20.     private String field3;
  21.     // 封裝處理結果的屬性
  22.     private int[] ints = { 1020 };
  23.     @SuppressWarnings("unchecked")
  24.     private Map map = new HashMap();
  25.     private String customName = "custom";
  26.     @SuppressWarnings("unchecked")
  27.     public String executeJson() {
  28.         map.put("name1""sinlff1");
  29.         map.put("name2""sinlff2");
  30.         map.put("name3""sinlff3");
  31.         return SUCCESS;
  32.     }
  33.     
  34.     // 三個請求參數對應的setter和getter方法
  35.     @JSON(serialize = false//format
  36.     public String getField1() {
  37.         return field1;
  38.     }
  39.     public void setField1(String field1) {
  40.         this.field1 = field1;
  41.     }
  42.     @JSON(serialize = false)
  43.     public String getField2() {
  44.         return field2;
  45.     }
  46.     public void setField2(String field2) {
  47.         this.field2 = field2;
  48.     }
  49.     @JSON(serialize = false//format
  50.     public String getField3() {
  51.         return field3;
  52.     }
  53.     public void setField3(String field3) {
  54.         this.field3 = field3;
  55.     }
  56.     public void setCustomName(String customName) {
  57.         this.customName = customName;
  58.     }
  59.     // 封裝處理結果的屬性的setter和getter方法
  60.     public int[] getInts() {
  61.         return ints;
  62.     }
  63.     public void setInts(int[] ints) {
  64.         this.ints = ints;
  65.     }
  66.     @SuppressWarnings("unchecked")
  67.     public Map getMap() {
  68.         return map;
  69.     }
  70.     @SuppressWarnings("unchecked")
  71.     public void setMap(Map map) {
  72.         this.map = map;
  73.     }
  74.     // 使用註釋語法來改變該屬性序列化後的屬性名
  75.     @JSON(name = "newName")
  76.     public String getCustomName() {
  77.         return this.customName;
  78.     }
  79. }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章