posts - 241,  comments - 116,  trackbacks - 0
          最近學(xué)習(xí)JSON的時(shí)候,數(shù)據(jù)傳輸問題。以前比較迷惑JSON類型的數(shù)據(jù),老是感覺就是JSON格式數(shù)據(jù)傳輸,不方便的很,也很反感。最近項(xiàng)目中用到了一個(gè)方法,所以就記錄一下,也順便接收接收大家的意見,完善代碼、提高自己技術(shù)。
          (注:我相信,在我之前肯定有人發(fā)過此類代碼,我并不與他人相比,只是自己的代碼,如果有哪些地方寫的不對,不合理的地方,大家指正,虛心受教,但是請不要謾罵?。?br />json-lib-2.4-jdk15.jar JSON轉(zhuǎn)換格式j(luò)ar包。 在JSONObject的官網(wǎng)中就有。
          Struts的配置
          <!--自定議返回類型-->
                  <result-types>
                      <result-type name="jsonResult" class="base.web.result.JsonResult"/>
                  </result-types>


          <action name="ajax" class="base.web.actions.week.WeekDataAction"
                          method="ajax">
                          <!-- jsonResult自定義返回類型, -->
                          <result name="success" type="jsonResult">
                          </result>
                  </action>

          1.基礎(chǔ)返回類型的抽象類 BaseResult

          (1)在定義struts的返回類型時(shí)候,則必須繼承StrutsResultSupport這個(gè)類

          (2)調(diào)用父類的構(gòu)造方法。

          package base.web.result;

          import org.apache.struts2.dispatcher.StrutsResultSupport;

          import com.opensymphony.xwork2.ActionInvocation;

          /**
           * <ol>
           * date:2011-10-15 editor:Yq
           * <li>創(chuàng)建文檔</li>
           * <li>返回結(jié)果類型基礎(chǔ)類</li>
           * </ol>
           * <ol>
           *
           * @author YeQing
           * @version 2.0
           * @since 1.6
           */
          public abstract class BaseResult extends StrutsResultSupport{

              public BaseResult() {
                  super();
              }
              
              public BaseResult(String location) {
                  super(location);
              }
              
              public boolean isAjax = false;
              
          }
          2.自定義返回類型的類 JsonResult
          package base.web.result;

          import java.io.PrintWriter;

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

          import net.sf.json.JSONObject;
          import net.sf.json.JsonConfig;

          import org.apache.struts2.ServletActionContext;

          import base.web.actions.BaseAction;

          import com.opensymphony.xwork2.ActionInvocation;

          /**
           * <ol>
           * date:2011-10-15 editor:Yq
           * <li>創(chuàng)建文檔</li>
           * <li>自定義返回類型JsonResult類</li>
           * </ol>
           * <ol>
           *
           * @author YeQing
           * @version 2.0
           * @since 1.6
           */
          public class JsonResult extends BaseResult{

              protected boolean isAjax = true;
              
              protected void doExecute(String arg0, ActionInvocation invocation)
                      throws Exception {
                  
                  //定義上下文的環(huán)境
                  ServletContext sc = ServletActionContext.getServletContext();
                  HttpServletRequest request = ServletActionContext.getRequest();
                  HttpServletResponse response = ServletActionContext.getResponse();
                  response.setCharacterEncoding(request.getCharacterEncoding());
                  System.out.println(request.getCharacterEncoding());
                  response.setContentType("application/json");//這里ContentType 需是application/json JS否則會(huì)出錯(cuò)。
                  
                  PrintWriter output = response.getWriter();//獲取響應(yīng)打印
                  BaseAction baseAction = (BaseAction) invocation.getAction();//獲得基礎(chǔ)Action類
                  
                  JSONObject js = new JSONObject();//JSON轉(zhuǎn)換為對象
                  js.put("model", baseAction.getModel());
                  /*此處js作為一個(gè)對象就像map一樣存儲(chǔ)自己的數(shù)據(jù)
                    baseAction.getModel()是actiond的基礎(chǔ)父類
                       所有的action都必須繼承BaseAction,BaseAction的變量 Map model = new HashMap();
                       則是所有action中數(shù)據(jù)存放的map, model則是放在struts2的值棧中的數(shù)據(jù)。
                       所有的數(shù)據(jù),都必須放在model里,然后在jsp頁面使用JSTL進(jìn)行輸出.
                               (也就是,說我將數(shù)據(jù)放入了model,然后將model放入了JSONObject,成為了JSON對象,
                                然后在js里獲得值,就像點(diǎn)運(yùn)算符一樣。)
          &nbsp;                */
                  
                  js.write(output);//打印
              }

          }
          3.JS異步請求,在回調(diào)函數(shù)中獲取數(shù)據(jù)。
          this.ajax=function() {
                  var request = new $WebRequest(basrUrl+"/psw/server/week/ajax.do",0,function(result)
                  {
                      alert(result.model.m.one);
                      //從回調(diào)函數(shù)的result中
                      //獲取存儲(chǔ)的model 然后獲取model中的m集合,
                                  //m.one map集合 中的oneKey 的值
                  });
                  request.Start();
              };
          4.補(bǔ)上WeekDataAction的方法。
          public String ajax() {
                  
                  System.out.println("start.......");
                  Map map = new HashMap();
                  map.put("one", 1);
                  map.put("two", 2);
                  model.put("m", map); //父類的變量
                  return getResult();
              }
          結(jié)束,有些地方時(shí)項(xiàng)目中內(nèi)容,牽扯類很多不好粘貼,如果大家有更好的辦法,告知一下,學(xué)習(xí)。
          posted on 2011-11-02 10:21 墻頭草 閱讀(5459) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運(yùn)專家
          主站蜘蛛池模板: 梅河口市| 临夏县| 大竹县| 丰都县| 垫江县| 车致| 宜阳县| 广宁县| 习水县| 保德县| 广德县| 辉南县| 额尔古纳市| 临清市| 商都县| 托克逊县| 焉耆| 疏勒县| 三穗县| 崇明县| 平凉市| 宜宾县| 五家渠市| 郸城县| 轮台县| 营口市| 濮阳县| 邢台县| 滁州市| 玉林市| 侯马市| 大邑县| 嘉黎县| 柯坪县| 东乡| 霍山县| 竹山县| 兴国县| 贵阳市| 项城市| 镇江市|