軟件是對質(zhì)量的不懈追求

          struts2 親密接觸 json(json result type)

          最近使用jquery,ajax調(diào)用后臺(tái)服務(wù)非常需要一個(gè)json返回類型,網(wǎng)上只有一個(gè)可用的插件,叫jsonplugin 。但是測試了一下,兩個(gè)版本居然都有錯(cuò)誤,而且通過文檔知道,它的使用還是相對復(fù)雜的,需要配置哪個(gè)對象需要轉(zhuǎn)換,那些對象不需要。不難想象,通常我們只需要將一個(gè)對象轉(zhuǎn)換成json格式,如果數(shù)據(jù)多,大不了都塞到一個(gè)對象里。
          這樣說起來,倒不如棄配置,轉(zhuǎn)而采用規(guī)則,規(guī)定action中如果需要為ajax提供服務(wù)器端服務(wù),必須定義一個(gè)名字叫json的成員類,類型當(dāng)然是最通用的Object。然后實(shí)現(xiàn)一個(gè)Result,在value stack中,找到名字叫json的對象,把它序列化成json串寫回客戶端就ok了。

            注意:如果json的實(shí)際類型是String,這個(gè)String必須符合json語法規(guī)范。Map List之類的就無所謂,直接用就是了

            Result代碼如下:

          import com.opensymphony.xwork2.ActionContext;
          import com.opensymphony.xwork2.ActionInvocation;
          import com.opensymphony.xwork2.Result;
          import com.opensymphony.xwork2.util.ValueStack;
          import net.sf.json.JSONObject;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          import org.apache.struts2.StrutsStatics;
          import javax.servlet.http.HttpServletResponse;
          import java.io.IOException;

          public class JSONResult implements Result {
             
              private static final Log log = LogFactory.getLog(JSONResult.class);

              
          public void execute(ActionInvocation invocation) throws Exception {
                  ActionContext actionContext 
          = invocation.getInvocationContext();
                 
                  HttpServletResponse response 
          = (HttpServletResponse) actionContext
                          .get(StrutsStatics.HTTP_RESPONSE);

                  
          try {
                      String json;
                      Object jsonObject;

                      
          // generate JSON
                     
                      ValueStack stack 
          = invocation.getStack();
                      jsonObject 
          = stack.findValue("json");
                      json 
          = JSONObject.fromObject(jsonObject).toString();
                     
                      log.debug(json);
                     
                      response.setContentType(
          "text/xml;charset=utf-8");
                      response.getWriter().write(json);

                  } 
          catch (IOException exception) {
                      log.error(exception.getMessage(), exception);
                      
          throw exception;
                  }
              }

          }
          struts配置如下:
            <result-types>
             
          <result-type name="json" class="JSONResult"/>
            
          </result-types>

          <action  >
              
          <result name="ajax" type="json" />
          </action>

          action部分代碼:
             

              
          private Object json;
              
          public Object getJson() {
                  
          return json;
              }

              
          public void setJson(Object json) {
                  
          this.json = json;
              }

          posted on 2009-05-14 15:20 BlakeSu 閱讀(5113) 評論(2)  編輯  收藏

          評論

          # re: struts2 親密接觸 json(json result type) 2011-06-15 00:53

          哈哈,謝謝吖!!!對幫助很大啊!!  回復(fù)  更多評論   

          # re: struts2 親密接觸 json(json result type) 2014-11-24 16:19 zhaobh

          很好啊  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 保亭| 军事| 麻城市| 光泽县| 杭锦后旗| 资兴市| 栾城县| 闽清县| 星子县| 吴堡县| 庆城县| 察哈| 易门县| 米泉市| 恭城| 永平县| 资源县| 太湖县| 绥化市| 通州区| 赫章县| 黑河市| 松潘县| 凌云县| 正定县| 鄢陵县| 永善县| 关岭| 睢宁县| 施甸县| 景谷| 新河县| 平阳县| 四川省| 新宾| 榆中县| 革吉县| 那坡县| 喜德县| 蒙山县| 汤阴县|