struts2 返回json資料__js

來源:互聯網
上載者:User
 

本例使用struts2版本 struts-2.1.8.1

 

首先匯入必要的jar包:


 

配置web.xml:

 

  Xml代碼   <filter>           <filter-name>struts2</filter-name>           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>       </filter>       <filter-mapping>           <filter-name>struts2</filter-name>           <url-pattern>*.action</url-pattern>       </filter-mapping>  

<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping>

 

 
 接著書寫Action類:

 

  Java代碼   import java.util.ArrayList;    import java.util.HashMap;    import java.util.List;    import java.util.Map;       import com.opensymphony.xwork2.Action;          public class Test {        public Map responseJson;        public Map getResponseJson() {            return responseJson;        }        public void setResponseJson(Map responseJson) {            this.responseJson = responseJson;        }                public String getList(){            Map<String, Object> map = new HashMap<String, Object>();            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();            for(int i=0;i<3;i++){                Map<String, Object> m = new HashMap<String, Object>();                m.put("id", i);                m.put("name", "Mic"+i);                list.add(m);            }            map.put("rows", list);            map.put("totalCont", 3);            this.setResponseJson(map);            return Action.SUCCESS;        }    }  

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.opensymphony.xwork2.Action;public class Test {public Map responseJson;public Map getResponseJson() {return responseJson;}public void setResponseJson(Map responseJson) {this.responseJson = responseJson;}public String getList(){Map<String, Object> map = new HashMap<String, Object>();List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();for(int i=0;i<3;i++){Map<String, Object> m = new HashMap<String, Object>();m.put("id", i);m.put("name", "Mic"+i);list.add(m);}map.put("rows", list);map.put("totalCont", 3);this.setResponseJson(map);return Action.SUCCESS;}}

 

 

最後配置struts.xml檔案:

 

  Xml代碼   <package name="jsonTest" extends="json-default">           <action name="test" class="bll.Test" method="getList">                            <result type="json">                   <param name="root">responseJson</param>               </result>           </action>       </package>  

<package name="jsonTest" extends="json-default"><action name="test" class="bll.Test" method="getList"><result type="json"><param name="root">responseJson</param></result></action></package>

 

struts.xml配置說明:

1,<result type="json"> 這句說明傳回型別為json,所以extends設定為"json-default"。

2,<param name="root">responseJson</param> responseJson對應返回資料的根,如果省略這行返回的結果如下:

  Js代碼   {"list":"success","responseJson":{"rows":[{"name":"Mic0","id":0},{"name":"Mic1","id":1},{"name":"Mic2","id":2}],"totalCont":3}}  

{"list":"success","responseJson":{"rows":[{"name":"Mic0","id":0},{"name":"Mic1","id":1},{"name":"Mic2","id":2}],"totalCont":3}}

 

 如果設定了root,結果如下:

  Js代碼   {"rows":[{"name":"Mic0","id":0},{"name":"Mic1","id":1},{"name":"Mic2","id":2}],"totalCont":3}  

{"rows":[{"name":"Mic0","id":0},{"name":"Mic1","id":1},{"name":"Mic2","id":2}],"totalCont":3}
 

以上步驟做好後,我們就可以調用該action了:

  Js代碼   Ext.onReady(function(){            new Mic.GridPanel({                title : 'test',                width : 500,                height : 410,                dataUrl : 'test.action',                dwrStore : false,                colMapping : ['id','name'],                headers : [                    { header: "Id", width: 60, dataIndex:'id'},                    { header: "名稱", width: 150, dataIndex:'name'}                ]            }).render(Ext.getBody());        });  

Ext.onReady(function(){  new Mic.GridPanel({title : 'test',width : 500,height : 410,        dataUrl : 'test.action',        dwrStore : false,        colMapping : ['id','name'],headers : [{ header: "Id", width: 60, dataIndex:'id'},        { header: "名稱", width: 150, dataIndex:'name'}]}).render(Ext.getBody());  });

 調用說明: dataUrl : 'test.action'  test就是action中的name屬性。

 

返回資料:


 

效果圖:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.