步骤1、写服务器端代码,输出json数据。 package com.voicesoft.agentbrowser.web.actions; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * @version 1.0 * @author Luo Yong <luoyong@voicesoft.cn> Created on : 2009-10-20, 14:28:47 * Copyright© 2009 Voicesoft Communications International Inc.All rights * reserved. Description:json Action 演示程序 */ public class JsonDemo extends ActionSupport { private String start; private String limit; private static List<Map> store; { store = new ArrayList<Map>(); for (int i = 0; i < 50; i++) { Map<String, String> record = new HashMap<String, String>(); record.put("id", i + ""); for (int j = 0; j < 3; j++) { record.put("key" + j, "值" + j); } store.add(record); } } private int results; @SuppressWarnings("unchecked") private List<Map> rows; public int getResults() { return results; } public void setResults(int results) { this.results = results; } public List<Map> getRows() { return rows; } public void setRows(List<Map> rows) { this.rows = rows; } @Override public String execute() throws Exception { setResults(store.size()); if (this.getResults() >= Integer.parseInt(start) + Integer.parseInt(limit)) { this.rows = store.subList(Integer.parseInt(start), Integer .parseInt(start) + Integer.parseInt(limit)); }else{ this.rows = store; } return SUCCESS; } public void setLimit(String limit) { this.limit = limit; } public void setStart(String start) { this.start = start; } } 步骤2、添加struts2配置文件struts.xml文件。 <package name="agnet-json" extends="json-default" namespace="/agent"> <action name="jsonDemo" class="com.voicesoft.agentbrowser.web.actions.JsonDemo"> <result type="json"> <param name="defaultEncoding">utf-8</param> </result> </action> </package> 步骤3、写extjs,显示数据。 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- struts 主配置文件,配置全局--> <struts> <!-- 国际化配置 --> <constant name="struts.custom.i18n.resources" value="message"/> <constant name="struts.i18n.encoding" value="utf8" /> <include file="struts_agent.xml"></include> <package name="myDefault" abstract="true" extends="struts-default"> <interceptors> <!-- 声明权限拦截器--> <interceptor name="securityInterceptor" class="com.voicesoft.Interceptor.SecurityInterceptor"/> <!-- 声明本系统默认截器栈--> <interceptor-stack name="myDefaultStack"> <interceptor-ref name="securityInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <!--设置本系统默认的拦截器--> <default-interceptor-ref name="myDefaultStack"/> <!-- 全局登录跳转页面--> <global-results> <result name="login" type="redirect">/login.jsp</result> <result name="appError" >/appError.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="appError" exception="com.voicesoft.agentbrowser.exceptions.ApplicationException"/> </global-exception-mappings> </package> <!--全局业务处理action包--> <package name="global-actions" extends="myDefault"> <!--用户登录--> <action name="LoginAction" class="loginAction"> <result name="success" type="redirect" >/agent/index.jsp</result> <result name="input" type="redirect" >/login.jsp</result> <result name="error" type="redirect" >/login.jsp</result> <interceptor-ref name="defaultStack"/> </action> <!--用户登出--> <action name="LogoutAction" class="logoutAction"> <result name="success" type="redirect" >/login.jsp</result> <interceptor-ref name="defaultStack"/> </action> </package> </struts>