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

          導航

          <2009年5月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          博客

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          struts2 親密接觸 json(json result type)

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

            注意:如果json的實際類型是String,這個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

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

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

          很好啊  回復  更多評論   


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 五寨县| 称多县| 延寿县| 宜良县| 游戏| 紫金县| 日土县| 翼城县| 麟游县| 左权县| 云安县| 仁寿县| 时尚| 枞阳县| 津市市| 大渡口区| 霍州市| 任丘市| 灌南县| 双鸭山市| 宿松县| 丰原市| 沾益县| 鄂州市| 普陀区| 兴城市| 扶风县| 永泰县| 高平市| 西乡县| 台前县| 玉田县| 松滋市| 柳河县| 铜梁县| 镇沅| 涟水县| 阳信县| 信宜市| 蒙自县| 宜良县|