struts2 json jquery 异步

2010-06-03

Struts2+jQuery+JSON实现异步交互

文章分类:Java编程

Struts2+JQuery+JSON实现异步交互,包括从后台获得单个值、对象、List和Map数据并从前台的表达中获取值把值进行序列化通过JQuery的$.ajax({})传到后台和后台的对象进行绑定。

第一步:在MyEclipse中创建JavaWeb工厂并把Struts2和json的jar包添加到工程中添加完成后的工程图是:

第二步:创建后台:

1.UserInfo实体类:

Userinfo实体类代码

1.packagestruts2jsonjquery.test.entity;

2.

3.importjava.io.Serializable;

4./**

5.*<p>

6.*用户实体类

7.*</p>

8.*@author朱延伟

9.*

10.*/

11.publicclassUserInfoimplementsSerializable{

12.

13.privatestaticfinallongserialVersionUID=3952189513312630860L;

14.

15.privateintuserId;

16.privateStringuserName;

17.privateStringpassword;

18.publicintgetUserId(){

19.returnuserId;

20.}

21.publicvoidsetUserId(intuserId){

22.this.userId=userId;

23.}

24.publicStringgetUserName(){

25.returnuserName;

26.}

27.publicvoidsetUserName(StringuserName){

28.this.userName=userName;

29.}

30.publicStringgetPassword(){

31.returnpassword;

32.}

33.publicvoidsetPassword(Stringpassword){

34.this.password=password;

35.}

36.}

Userinfo实体类代码

1.packagestruts2jsonjquery.test.entity;

2.

3.importjava.io.Serializable;

4./**

5.*<p>

6.*用户实体类

7.*</p>

8.*@author朱延伟

9.*

10.*/

11.publicclassUserInfoimplementsSerializable{

12.

13.privatestaticfinallongserialVersionUID=3952189513312630860L;

14.

15.privateintuserId;

16.privateStringuserName;

17.privateStringpassword;

18.publicintgetUserId(){

19.returnuserId;

20.}

21.publicvoidsetUserId(intuserId){

22.this.userId=userId;

23.}

24.publicStringgetUserName(){

25.returnuserName;

26.}

27.publicvoidsetUserName(StringuserName){

28.this.userName=userName;

29.}

30.publicStringgetPassword(){

31.returnpassword;

32.}

33.publicvoidsetPassword(Stringpassword){

34.this.password=password;

35.}

36.}

packagestruts2jsonjquery.test.entity;

importjava.io.Serializable;

/**

*<p>

*用户实体类

*</p>

*@author朱延伟

*

*/

publicclassUserInfoimplementsSerializable{

privatestaticfinallongserialVersionUID=3952189513312630860L;

privateintuserId;

privateStringuserName;

privateStringpassword;

publicintgetUserId(){

returnuserId;

}

publicvoidsetUserId(intuserId){

this.userId=userId;

}

publicStringgetUserName(){

returnuserName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

}

2.Action类

Action类代码

1.packagestruts2jsonjquery.test.action;

2.

3.importjava.util.ArrayList;

4.importjava.util.HashMap;

5.importjava.util.List;

6.importjava.util.Map;

7.

8.importstruts2jsonjquery.test.entity.UserInfo;

9.

10.importcom.opensymphony.xwork2.ActionSupport;

11.

12.publicclassJsonJqueryStruts2ActionextendsActionSupport{

13.

14.privatestaticfinallongserialVersionUID=3518833679938898354L;

15.

16.privateStringmessage;//使用json返回单个值

17.privateUserInfouserInfo;//使用json返回对象

18.privateList<UserInfo>userInfosList;//使用josn返回List对象

19.privateMap<String,UserInfo>userInfosMap;//使用json返回Map对象

20.//为上面的的属性提供get,Set方法

21.publicStringgetMessage(){

22.returnmessage;

23.}

24.publicvoidsetMessage(Stringmessage){

25.this.message=message;

26.}

27.publicUserInfogetUserInfo(){

28.returnuserInfo;

29.}

30.publicvoidsetUserInfo(UserInfouserInfo){

31.this.userInfo=userInfo;

32.}

33.publicList<UserInfo>getUserInfosList(){

34.returnuserInfosList;

35.}

36.publicvoidsetUserInfosList(List<UserInfo>userInfosList){

37.this.userInfosList=userInfosList;

38.}

39.publicMap<String,UserInfo>getUserInfosMap(){

40.returnuserInfosMap;

41.}

42.publicvoidsetUserInfosMap(Map<String,UserInfo>userInfosMap){

43.this.userInfosMap=userInfosMap;

44.}

45./**

46.*<p>

47.*返回单个值

48.*<p>

49.*@return

50.*/

51.publicStringreturnMessage(){

52.this.message="成功返回单个值";

53.return"message";

54.}

55./**

56.*<p>

57.*返回UserInfo对象

58.*</p>

59.*@return

60.*/

61.publicStringreturnUserInfo(){

62.userInfo=newUserInfo();

63.userInfo.setUserId(10000);

64.userInfo.setUserName("张三");

65.userInfo.setPassword("000000");

66.return"userInfo";

67.}

68./**

69.*<p>

70.*返回List对象

71.*</p>

72.*@return

73.*/

74.publicStringreturnList(){

75.userInfosList=newArrayList<UserInfo>();

76.UserInfou1=newUserInfo();

77.u1.setUserId(10000);

78.u1.setUserName("张三");

79.u1.setPassword("000000");

80.UserInfou2=newUserInfo();

81.u2.setUserId(10001);

82.u2.setUserName("李四");

83.u2.setPassword("111111");

84.UserInfou3=newUserInfo();

85.u3.setUserId(10002);

86.u3.setUserName("王五");

87.u3.setPassword("222222");

88.UserInfou4=newUserInfo();

89.u4.setUserId(10003);

90.u4.setUserName("赵六");

91.u4.setPassword("333333");

92.userInfosList.add(u1);

93.userInfosList.add(u2);

94.userInfosList.add(u3);

95.userInfosList.add(u4);

96.return"list";

97.}

98./**

99.*<p>

100.*返回Map对象

101.*</p>

102.*@return

103.*/

104.publicStringreturnMap(){

105.userInfosMap=newHashMap<String,UserInfo>();

106.UserInfou1=newUserInfo();

107.u1.setUserId(10000);

108.u1.setUserName("张三");

109.u1.setPassword("000000");

110.UserInfou2=newUserInfo();

111.u2.setUserId(10001);

112.u2.setUserName("李四");

113.u2.setPassword("111111");

114.UserInfou3=newUserInfo();

115.u3.setUserId(10002);

116.u3.setUserName("王五");

117.u3.setPassword("222222");

118.UserInfou4=newUserInfo();

119.u4.setUserId(10003);

120.u4.setUserName("赵六");

121.u4.setPassword("333333");

122.userInfosMap.put(u1.getUserId()+"",u1);

123.userInfosMap.put(u2.getUserId()+"",u2);

124.userInfosMap.put(u3.getUserId()+"",u3);

125.userInfosMap.put(u4.getUserId()+"",u4);

126.return"map";

127.}

128./**

129.*<p>

130.*获得对象,也就是通过表达获得对象(异步的)

131.*</P>

132.*@return

133.*/

134.publicStringgainUserInfo(){

135.System.out.println("用户ID:"+userInfo.getUserId());

136.System.out.println("用户名:"+userInfo.getUserName());

137.System.out.println("密码:"+userInfo.getPassword());

138.return"userInfo";

139.}

140./**

141.*获得单个值就不用写了和平常一样

142.*/

143.}

Action类代码

1.packagestruts2jsonjquery.test.action;

2.

3.importjava.util.ArrayList;

4.importjava.util.HashMap;

5.importjava.util.List;

6.importjava.util.Map;

7.

8.importstruts2jsonjquery.test.entity.UserInfo;

9.

10.importcom.opensymphony.xwork2.ActionSupport;

11.

12.publicclassJsonJqueryStruts2ActionextendsActionSupport{

13.

14.privatestaticfinallongserialVersionUID=3518833679938898354L;

15.

16.privateStringmessage;//使用json返回单个值

17.privateUserInfouserInfo;//使用json返回对象

18.privateList<UserInfo>userInfosList;//使用josn返回List对象

19.privateMap<String,UserInfo>userInfosMap;//使用json返回Map对象

20.//为上面的的属性提供get,Set方法

21.publicStringgetMessage(){

22.returnmessage;

23.}

24.publicvoidsetMessage(Stringmessage){

25.this.message=message;

26.}

27.publicUserInfogetUserInfo(){

28.returnuserInfo;

29.}

30.publicvoidsetUserInfo(UserInfouserInfo){

31.this.userInfo=userInfo;

32.}

33.publicList<UserInfo>getUserInfosList(){

34.returnuserInfosList;

35.}

36.publicvoidsetUserInfosList(List<UserInfo>userInfosList){

37.this.userInfosList=userInfosList;

38.}

39.publicMap<String,UserInfo>getUserInfosMap(){

40.returnuserInfosMap;

41.}

42.publicvoidsetUserInfosMap(Map<String,UserInfo>userInfosMap){

43.this.userInfosMap=userInfosMap;

44.}

45./**

46.*<p>

47.*返回单个值

48.*<p>

49.*@return

50.*/

51.publicStringreturnMessage(){

52.this.message="成功返回单个值";

53.return"message";

54.}

55./**

56.*<p>

57.*返回UserInfo对象

58.*</p>

59.*@return

60.*/

61.publicStringreturnUserInfo(){

62.userInfo=newUserInfo();

63.userInfo.setUserId(10000);

64.userInfo.setUserName("张三");

65.userInfo.setPassword("000000");

66.return"userInfo";

67.}

68./**

69.*<p>

70.*返回List对象

71.*</p>

72.*@return

73.*/

74.publicStringreturnList(){

75.userInfosList=newArrayList<UserInfo>();

76.UserInfou1=newUserInfo();

77.u1.setUserId(10000);

78.u1.setUserName("张三");

79.u1.setPassword("000000");

80.UserInfou2=newUserInfo();

81.u2.setUserId(10001);

82.u2.setUserName("李四");

83.u2.setPassword("111111");

84.UserInfou3=newUserInfo();

85.u3.setUserId(10002);

86.u3.setUserName("王五");

87.u3.setPassword("222222");

88.UserInfou4=newUserInfo();

89.u4.setUserId(10003);

90.u4.setUserName("赵六");

91.u4.setPassword("333333");

92.userInfosList.add(u1);

93.userInfosList.add(u2);

94.userInfosList.add(u3);

95.userInfosList.add(u4);

96.return"list";

97.}

98./**

99.*<p>

100.*返回Map对象

101.*</p>

102.*@return

103.*/

104.publicStringreturnMap(){

105.userInfosMap=newHashMap<String,UserInfo>();

106.UserInfou1=newUserInfo();

107.u1.setUserId(10000);

108.u1.setUserName("张三");

109.u1.setPassword("000000");

110.UserInfou2=newUserInfo();

111.u2.setUserId(10001);

112.u2.setUserName("李四");

113.u2.setPassword("111111");

114.UserInfou3=newUserInfo();

115.u3.setUserId(10002);

116.u3.setUserName("王五");

117.u3.setPassword("222222");

118.UserInfou4=newUserInfo();

119.u4.setUserId(10003);

120.u4.setUserName("赵六");

121.u4.setPassword("333333");

122.userInfosMap.put(u1.getUserId()+"",u1);

123.userInfosMap.put(u2.getUserId()+"",u2);

124.userInfosMap.put(u3.getUserId()+"",u3);

125.userInfosMap.put(u4.getUserId()+"",u4);

126.return"map";

127.}

128./**

129.*<p>

130.*获得对象,也就是通过表达获得对象(异步的)

131.*</P>

132.*@return

133.*/

134.publicStringgainUserInfo(){

135.System.out.println("用户ID:"+userInfo.getUserId());

136.System.out.println("用户名:"+userInfo.getUserName());

137.System.out.println("密码:"+userInfo.getPassword());

138.return"userInfo";

139.}

140./**

141.*获得单个值就不用写了和平常一样

142.*/

143.}

packagestruts2jsonjquery.test.action;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importstruts2jsonjquery.test.entity.UserInfo;

importcom.opensymphony.xwork2.ActionSupport;

publicclassJsonJqueryStruts2ActionextendsActionSupport{

privatestaticfinallongserialVersionUID=3518833679938898354L;

privateStringmessage;//使用json返回单个值

privateUserInfouserInfo;//使用json返回对象

privateList<UserInfo>userInfosList;//使用josn返回List对象

privateMap<String,UserInfo>userInfosMap;//使用json返回Map对象

//为上面的的属性提供get,Set方法

publicStringgetMessage(){

returnmessage;

}

publicvoidsetMessage(Stringmessage){

this.message=message;

}

publicUserInfogetUserInfo(){

returnuserInfo;

}

publicvoidsetUserInfo(UserInfouserInfo){

this.userInfo=userInfo;

}

publicList<UserInfo>getUserInfosList(){

returnuserInfosList;

}

publicvoidsetUserInfosList(List<UserInfo>userInfosList){

this.userInfosList=userInfosList;

}

publicMap<String,UserInfo>getUserInfosMap(){

returnuserInfosMap;

}

publicvoidsetUserInfosMap(Map<String,UserInfo>userInfosMap){

this.userInfosMap=userInfosMap;

}

/**

*<p>

*返回单个值

*<p>

*@return

*/

publicStringreturnMessage(){

this.message="成功返回单个值";

return"message";

}

/**

*<p>

*返回UserInfo对象

*</p>

*@return

*/

publicStringreturnUserInfo(){

userInfo=newUserInfo();

userInfo.setUserId(10000);

userInfo.setUserName("张三");

userInfo.setPassword("000000");

return"userInfo";

}

/**

*<p>

*返回List对象

*</p>

*@return

*/

publicStringreturnList(){

userInfosList=newArrayList<UserInfo>();

UserInfou1=newUserInfo();

u1.setUserId(10000);

u1.setUserName("张三");

u1.setPassword("000000");

UserInfou2=newUserInfo();

u2.setUserId(10001);

u2.setUserName("李四");

u2.setPassword("111111");

UserInfou3=newUserInfo();

u3.setUserId(10002);

u3.setUserName("王五");

u3.setPassword("222222");

UserInfou4=newUserInfo();

u4.setUserId(10003);

u4.setUserName("赵六");

u4.setPassword("333333");

userInfosList.add(u1);

userInfosList.add(u2);

userInfosList.add(u3);

userInfosList.add(u4);

return"list";

}

/**

*<p>

*返回Map对象

*</p>

*@return

*/

publicStringreturnMap(){

userInfosMap=newHashMap<String,UserInfo>();

UserInfou1=newUserInfo();

u1.setUserId(10000);

u1.setUserName("张三");

u1.setPassword("000000");

UserInfou2=newUserInfo();

u2.setUserId(10001);

u2.setUserName("李四");

u2.setPassword("111111");

UserInfou3=newUserInfo();

u3.setUserId(10002);

u3.setUserName("王五");

u3.setPassword("222222");

UserInfou4=newUserInfo();

u4.setUserId(10003);

u4.setUserName("赵六");

u4.setPassword("333333");

userInfosMap.put(u1.getUserId()+"",u1);

userInfosMap.put(u2.getUserId()+"",u2);

userInfosMap.put(u3.getUserId()+"",u3);

userInfosMap.put(u4.getUserId()+"",u4);

return"map";

}

/**

*<p>

*获得对象,也就是通过表达获得对象(异步的)

*</P>

*@return

*/

publicStringgainUserInfo(){

System.out.println("用户ID:"+userInfo.getUserId());

System.out.println("用户名:"+userInfo.getUserName());

System.out.println("密码:"+userInfo.getPassword());

return"userInfo";

}

/**

*获得单个值就不用写了和平常一样

*/

}

3.struts.xml

Struts.xml代码

1.<?xmlversion="1.0"encoding="UTF-8"?>

2.<!DOCTYPEstrutsPUBLIC

3."-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

4."http://struts.apache.org/dtds/struts-2.0.dtd">

5.

6.<struts>

7.

8.<packagename="default"namespace="/"extends="json-default">

9.<actionname="jsontest"class="struts2jsonjquery.test.action.JsonJqueryStruts2Action">

10.<!--返回单个值的result-->

11.<resultname="message"type="json"></result>

12.<!--返回UserInfo对象的-->

13.<resultname="userInfo"type="json"></result>

14.<!--返回List对象的-->

15.<resultname="list"type="json"></result>

16.<!--返回Map对象的-->

17.<resultname="map"type="json"></result>

18.</action>

19.</package>

20.</struts>

Struts.xml代码

1.<?xmlversion="1.0"encoding="UTF-8"?>

2.<!DOCTYPEstrutsPUBLIC

3."-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

4."http://struts.apache.org/dtds/struts-2.0.dtd">

5.

6.<struts>

7.

8.<packagename="default"namespace="/"extends="json-default">

9.<actionname="jsontest"class="struts2jsonjquery.test.action.JsonJqueryStruts2Action">

10.<!--返回单个值的result-->

11.<resultname="message"type="json"></result>

12.<!--返回UserInfo对象的-->

13.<resultname="userInfo"type="json"></result>

14.<!--返回List对象的-->

15.<resultname="list"type="json"></result>

16.<!--返回Map对象的-->

17.<resultname="map"type="json"></result>

18.</action>

19.</package>

20.</struts>

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<packagename="default"namespace="/"extends="json-default">

<actionname="jsontest"class="struts2jsonjquery.test.action.JsonJqueryStruts2Action">

<!--返回单个值的result-->

<resultname="message"type="json"></result>

<!--返回UserInfo对象的-->

<resultname="userInfo"type="json"></result>

<!--返回List对象的-->

<resultname="list"type="json"></result>

<!--返回Map对象的-->

<resultname="map"type="json"></result>

</action>

</package>

</struts>

前台:

1.index.jsp

Html代码

1.<%@pagelanguage="java"pageEncoding="GBK"%>

2.<%

3.Stringpath=request.getContextPath();

4.%>

5.

6.<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

7.<html>

8.<head>

9.

10.<title>Struts2+JQuery+JSON</title>

11.<metahttp-equiv="pragma"content="no-cache">

12.<metahttp-equiv="cache-control"content="no-cache">

13.<metahttp-equiv="expires"content="0">

14.<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

15.<metahttp-equiv="description"content="Thisismypage">

16.<scripttype="text/javascript"src="<%=path%>/js/jquery.js"></script>

17.<scripttype="text/javascript"src="<%=path%>/js/json.js"></script>

18.</head>

19.

20.<body>

21.<inputid="getMessage"type="button"value="获取单个值"/>&nbsp;&nbsp;

22.<inputid="getUserInfo"type="button"value="获取UserInfo对象"/>&nbsp;&nbsp;

23.<inputid="getList"type="button"value="获取List对象"/>&nbsp;&nbsp;

24.<inputid="getMap"type="button"value="获取Map对象"/>&nbsp;&nbsp;

25.<br>

26.<br>

27.<br>

28.<!--要显示信息的层-->

29.<divid="message"></div>

30.<form>

31.用户ID:<inputname="userInfo.userId"type="text"/><br/>

32.用户名:<inputname="userInfo.userName"type="text"/><br/>

33.密&nbsp;&nbsp;&nbsp;码:<inputname="userInfo.password"type="text"/><br>

34.<inputid="regRe"type="button"value="注册"/>

35.</form>

36.</body>

37.</html>

Html代码

1.<%@pagelanguage="java"pageEncoding="GBK"%>

2.<%

3.Stringpath=request.getContextPath();

4.%>

5.

6.<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

7.<html>

8.<head>

9.

10.<title>Struts2+JQuery+JSON</title>

11.<metahttp-equiv="pragma"content="no-cache">

12.<metahttp-equiv="cache-control"content="no-cache">

13.<metahttp-equiv="expires"content="0">

14.<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

15.<metahttp-equiv="description"content="Thisismypage">

16.<scripttype="text/javascript"src="<%=path%>/js/jquery.js"></script>

17.<scripttype="text/javascript"src="<%=path%>/js/json.js"></script>

18.</head>

19.

20.<body>

21.<inputid="getMessage"type="button"value="获取单个值"/>&nbsp;&nbsp;

22.<inputid="getUserInfo"type="button"value="获取UserInfo对象"/>&nbsp;&nbsp;

23.<inputid="getList"type="button"value="获取List对象"/>&nbsp;&nbsp;

24.<inputid="getMap"type="button"value="获取Map对象"/>&nbsp;&nbsp;

25.<br>

26.<br>

27.<br>

28.<!--要显示信息的层-->

29.<divid="message"></div>

30.<form>

31.用户ID:<inputname="userInfo.userId"type="text"/><br/>

32.用户名:<inputname="userInfo.userName"type="text"/><br/>

33.密&nbsp;&nbsp;&nbsp;码:<inputname="userInfo.password"type="text"/><br>

34.<inputid="regRe"type="button"value="注册"/>

35.</form>

36.</body>

37.</html>

<%@pagelanguage="java"pageEncoding="GBK"%>

<%

Stringpath=request.getContextPath();

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<title>Struts2+JQuery+JSON</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

<scripttype="text/javascript"src="<%=path%>/js/jquery.js"></script>

<scripttype="text/javascript"src="<%=path%>/js/json.js"></script>

</head>

<body>

<inputid="getMessage"type="button"value="获取单个值"/>&nbsp;&nbsp;

<inputid="getUserInfo"type="button"value="获取UserInfo对象"/>&nbsp;&nbsp;

<inputid="getList"type="button"value="获取List对象"/>&nbsp;&nbsp;

<inputid="getMap"type="button"value="获取Map对象"/>&nbsp;&nbsp;

<br>

<br>

<br>

<!--要显示信息的层-->

<divid="message"></div>

<form>

用户ID:<inputname="userInfo.userId"type="text"/><br/>

用户名:<inputname="userInfo.userName"type="text"/><br/>

密&nbsp;&nbsp;&nbsp;码:<inputname="userInfo.password"type="text"/><br>

<inputid="regRe"type="button"value="注册"/>

</form>

</body>

</html>

2.json.js

Js代码

1.//初始加载页面时

2.$(document).ready(function(){

3.//为获取单个值的按钮注册鼠标单击事件

4.$("#getMessage").click(function(){

5.$.getJSON("jsontest!returnMessage.action",function(data){

6.//通过.操作符可以从data.message中获得Action中message的值

7.$("#message").html("<fontcolor='red'>"+data.message+"</font>");

8.});

9.});

10.//为获取UserInfo对象按钮添加鼠标单击事件

11.$("#getUserInfo").click(function(){

12.$.getJSON("jsontest!returnUserInfo.action",function(data){

13.//清空显示层中的数据

14.$("#message").html("");

15.//为显示层添加获取到的数据

16.//获取对象的数据用data.userInfo.属性

17.$("#message").append("<div><fontcolor='red'>用户ID:"+data.userInfo.userId+"</font></div>")

18..append("<div><fontcolor='red'>用户名:"+data.userInfo.userName+"</font></div>")

19..append("<div><fontcolor='red'>密码:"+data.userInfo.password+"</font></div>")

20.});

21.});

22.//为获取List对象按钮添加鼠标单击事件

23.$("#getList").click(function(){

24.$.getJSON("jsontest!returnList.action",function(data){

25.//清空显示层中的数据

26.$("#message").html("");

27.//使用jQuery中的each(data,function(){});函数

28.//从data.userInfosList获取UserInfo对象放入value之中

29.$.each(data.userInfosList,function(i,value){

30.$("#message").append("<div>第"+(i+1)+"个用户:</div>")

31..append("<div><fontcolor='red'>用户ID:"+value.userId+"</font></div>")

32..append("<div><fontcolor='red'>用户名:"+value.userName+"</font></div>")

33..append("<div><fontcolor='red'>密码:"+value.password+"</font></div>");

34.});

35.});

36.});

37.//为获取Map对象按钮添加鼠标单击事件

38.$("#getMap").click(function(){

39.$.getJSON("jsontest!returnMap.action",function(data){

40.//清空显示层中的数据

41.$("#message").html("");

42.//使用jQuery中的each(data,function(){});函数

43.//从data.userInfosList获取UserInfo对象放入value之中

44.//key值为Map的键值

45.$.each(data.userInfosMap,function(key,value){

46.$("#message").append("<div><fontcolor='red'>用户ID:"+value.userId+"</font></div>")

47..append("<div><fontcolor='red'>用户名:"+value.userName+"</font></div>")

48..append("<div><fontcolor='red'>密码:"+value.password+"</font></div>");

49.});

50.});

51.});

52.//向服务器发送表达数据

53.$("#regRe").click(function(){

54.//把表单的数据进行序列化

55.varparams=$("form").serialize();

56.//使用jQuery中的$.ajax({});Ajax方法

57.$.ajax({

58.url:"jsontest!gainUserInfo.action",

59.type:"POST",

60.data:params,

61.dataType:"json",

62.success:function(data){

63.//清空显示层中的数据

64.$("#message").html("");

65.//为显示层添加获取到的数据

66.//获取对象的数据用data.userInfo.属性

67.$("#message").append("<div><fontcolor='red'>用户ID:"+data.userInfo.userId+"</font></div>")

68..append("<div><fontcolor='red'>用户名:"+data.userInfo.userName+"</font></div>")

69..append("<div><fontcolor='red'>密码:"+data.userInfo.password+"</font></div>")

70.}

71.});

72.});

73.});

Js代码

1.//初始加载页面时

2.$(document).ready(function(){

3.//为获取单个值的按钮注册鼠标单击事件

4.$("#getMessage").click(function(){

5.$.getJSON("jsontest!returnMessage.action",function(data){

6.//通过.操作符可以从data.message中获得Action中message的值

7.$("#message").html("<fontcolor='red'>"+data.message+"</font>");

8.});

9.});

10.//为获取UserInfo对象按钮添加鼠标单击事件

11.$("#getUserInfo").click(function(){

12.$.getJSON("jsontest!returnUserInfo.action",function(data){

13.//清空显示层中的数据

14.$("#message").html("");

15.//为显示层添加获取到的数据

16.//获取对象的数据用data.userInfo.属性

17.$("#message").append("<div><fontcolor='red'>用户ID:"+data.userInfo.userId+"</font></div>")

18..append("<div><fontcolor='red'>用户名:"+data.userInfo.userName+"</font></div>")

19..append("<div><fontcolor='red'>密码:"+data.userInfo.password+"</font></div>")

20.});

21.});

22.//为获取List对象按钮添加鼠标单击事件

23.$("#getList").click(function(){

24.$.getJSON("jsontest!returnList.action",function(data){

25.//清空显示层中的数据

26.$("#message").html("");

27.//使用jQuery中的each(data,function(){});函数

28.//从data.userInfosList获取UserInfo对象放入value之中

29.$.each(data.userInfosList,function(i,value){

30.$("#message").append("<div>第"+(i+1)+"个用户:</div>")

31..append("<div><fontcolor='red'>用户ID:"+value.userId+"</font></div>")

32..append("<div><fontcolor='red'>用户名:"+value.userName+"</font></div>")

33..append("<div><fontcolor='red'>密码:"+value.password+"</font></div>");

34.});

35.});

36.});

37.//为获取Map对象按钮添加鼠标单击事件

38.$("#getMap").click(function(){

39.$.getJSON("jsontest!returnMap.action",function(data){

40.//清空显示层中的数据

41.$("#message").html("");

42.//使用jQuery中的each(data,function(){});函数

43.//从data.userInfosList获取UserInfo对象放入value之中

44.//key值为Map的键值

45.$.each(data.userInfosMap,function(key,value){

46.$("#message").append("<div><fontcolor='red'>用户ID:"+value.userId+"</font></div>")

47..append("<div><fontcolor='red'>用户名:"+value.userName+"</font></div>")

48..append("<div><fontcolor='red'>密码:"+value.password+"</font></div>");

49.});

50.});

51.});

52.//向服务器发送表达数据

53.$("#regRe").click(function(){

54.//把表单的数据进行序列化

55.varparams=$("form").serialize();

56.//使用jQuery中的$.ajax({});Ajax方法

57.$.ajax({

58.url:"jsontest!gainUserInfo.action",

59.type:"POST",

60.data:params,

61.dataType:"json",

62.success:function(data){

63.//清空显示层中的数据

64.$("#message").html("");

65.//为显示层添加获取到的数据

66.//获取对象的数据用data.userInfo.属性

67.$("#message").append("<div><fontcolor='red'>用户ID:"+data.userInfo.userId+"</font></div>")

68..append("<div><fontcolor='red'>用户名:"+data.userInfo.userName+"</font></div>")

69..append("<div><fontcolor='red'>密码:"+data.userInfo.password+"</font></div>")

70.}

71.});

72.});

73.});

//初始加载页面时

$(document).ready(function(){

//为获取单个值的按钮注册鼠标单击事件

$("#getMessage").click(function(){

$.getJSON("jsontest!returnMessage.action",function(data){

//通过.操作符可以从data.message中获得Action中message的值

$("#message").html("<fontcolor='red'>"+data.message+"</font>");

});

});

//为获取UserInfo对象按钮添加鼠标单击事件

$("#getUserInfo").click(function(){

$.getJSON("jsontest!returnUserInfo.action",function(data){

//清空显示层中的数据

$("#message").html("");

//为显示层添加获取到的数据

//获取对象的数据用data.userInfo.属性

$("#message").append("<div><fontcolor='red'>用户ID:"+data.userInfo.userId+"</font></div>")

.append("<div><fontcolor='red'>用户名:"+data.userInfo.userName+"</font></div>")

.append("<div><fontcolor='red'>密码:"+data.userInfo.password+"</font></div>")

});

});

//为获取List对象按钮添加鼠标单击事件

$("#getList").click(function(){

$.getJSON("jsontest!returnList.action",function(data){

//清空显示层中的数据

$("#message").html("");

//使用jQuery中的each(data,function(){});函数

//从data.userInfosList获取UserInfo对象放入value之中

$.each(data.userInfosList,function(i,value){

$("#message").append("<div>第"+(i+1)+"个用户:</div>")

.append("<div><fontcolor='red'>用户ID:"+value.userId+"</font></div>")

.append("<div><fontcolor='red'>用户名:"+value.userName+"</font></div>")

.append("<div><fontcolor='red'>密码:"+value.password+"</font></div>");

});

});

});

//为获取Map对象按钮添加鼠标单击事件

$("#getMap").click(function(){

$.getJSON("jsontest!returnMap.action",function(data){

//清空显示层中的数据

$("#message").html("");

//使用jQuery中的each(data,function(){});函数

//从data.userInfosList获取UserInfo对象放入value之中

//key值为Map的键值

$.each(data.userInfosMap,function(key,value){

$("#message").append("<div><fontcolor='red'>用户ID:"+value.userId+"</font></div>")

.append("<div><fontcolor='red'>用户名:"+value.userName+"</font></div>")

.append("<div><fontcolor='red'>密码:"+value.password+"</font></div>");

});

});

});

//向服务器发送表达数据

$("#regRe").click(function(){

//把表单的数据进行序列化

varparams=$("form").serialize();

//使用jQuery中的$.ajax({});Ajax方法

$.ajax({

url:"jsontest!gainUserInfo.action",

type:"POST",

data:params,

dataType:"json",

success:function(data){

//清空显示层中的数据

$("#message").html("");

//为显示层添加获取到的数据

//获取对象的数据用data.userInfo.属性

$("#message").append("<div><fontcolor='red'>用户ID:"+data.userInfo.userId+"</font></div>")

.append("<div><fontcolor='red'>用户名:"+data.userInfo.userName+"</font></div>")

.append("<div><fontcolor='red'>密码:"+data.userInfo.password+"</font></div>")

}

});

});

});注意:JSON插件会把所有包含getter方法的属性都序列化到返回结果中,所以Action中的其他execute方法都不要把名

字加get前缀。

可以下载工程文件

相关推荐