该用户从未签到
|
一、包$ d+ ?) ^5 ]$ c; j. x/ E, B
struts2与json的依赖包:struts2-json-plugin-2.2.3.jar
O: |6 `- [1 G/ x) X二、过程:6 u; [. x/ k& l: J" o) [
①引入json依赖包
1 @2 w9 D! B, L②编写action类
& e, @) K0 t+ c# V5 f& H7 o③配置struts.xml3 x" L. _; W) V; U7 v; ~8 t8 A3 j
④编写页面
: X, S( F' P% q3 Y+ u⑤测试8 d5 v0 @1 n8 O$ q9 G- n
三、demo
# Y; R' e' F- u7 S6 |1、Action 类
+ [( e( o& Q; `
. ~& [/ K* i. t- tpackage com.zxt.action;import com.opensymphony.xwork2.ActionSupport;/** * * @Title: JsonAction.java * @Package com.zxt.action * @Description:struts2 + ajax + json用例 * @author zxt * @date 2011-12-6 上午10:38:51 * @version V1.0 */public class JsonAction extends ActionSupport { /** * */ private static final long serialVersionUID = 7443363719737618408L; /** * 姓名 */ private String name; /** * 身高 */ private String inch; /** * ajax返回结果,也可是其他类型的,这里以String类型为例 */ private String result; @Override public String execute() throws Exception { // TODO Auto-generated method stub if ("张三".equals(name)) { result = "身份验证通过,身高为" + inch; } else result = "不是张三!"; return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInch() { return inch; } public void setInch(String inch) { this.inch = inch; } /** * * @Title: getResult * @Description:json调取结果 * @param @return * @return String * @throws */ public String getResult() { return result; }}
- n. \3 h9 i. P4 i4 _5 o9 U; U; @" S2、Struts 配置' z% }& a; _% }5 e7 p
<package name="ajax" extends="json-default"> <action name="jsonAjax" class="com.zxt.action.JsonAction"> <!-- 将返回类型设置为json --> <result type="json"></result> </action></package>3 _# ^) y; w- J8 y6 c& X
% k3 C$ r( G/ u( B q7 y7 x |
-
总评分: 帮币 + 5
查看全部评分
|