日期:2014-05-16  浏览次数:20487 次

Struts2+jQuery+JSON实现异步交互

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

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

?

第二步:创建后台:

? 1. UserInfo实体类:

?

Userinfo实体类代码 复制代码
  1. package?struts2jsonjquery.test.entity; ??
  2. ??
  3. import?java.io.Serializable; ??
  4. /** ??
  5. ?*?<p> ??
  6. ?*??用户实体类 ??
  7. ?*?</p> ??
  8. ?*?@author?朱延伟 ??
  9. ?* ??
  10. ?*/ ??
  11. public?class?UserInfo?implements?Serializable?{ ??
  12. ??
  13. ????private?static?final?long?serialVersionUID?=?3952189513312630860L; ??
  14. ???? ??
  15. ????private?int?userId; ??
  16. ????private?String?userName; ??
  17. ????private?String?password; ??
  18. ????public?int?getUserId()?{ ??
  19. ????????return?userId; ??
  20. ????} ??
  21. ????public?void?setUserId(int?userId)?{ ??
  22. ????????this.userId?=?userId; ??
  23. ????} ??
  24. ????public?String?getUserName()?{ ??
  25. ????????return?userName; ??
  26. ????} ??
  27. ????public?void?setUserName(String?userName)?{ ??
  28. ????????this.userName?=?userName; ??
  29. ????} ??
  30. ????public?String?getPassword()?{ ??
  31. ????????return?password; ??
  32. ????} ??
  33. ????public?void?setPassword(String?password)?{ ??
  34. ????????this.password?=?password; ??
  35. ????} ??
  36. }??
package struts2jsonjquery.test.entity;

import java.io.Serializable;
/**
 * <p>
 * 	用户实体类
 * </p>
 * @author 朱延伟
 *
 */
public class UserInfo implements Serializable {

	private static final long serialVersionUID = 3952189513312630860L;
	
	private int userId;
	private String userName;
	private String password;
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

?

2. Action类

Action类代码 复制代码
  1. package?struts2jsonjquery.test.action; ??
  2. ??
  3. import?java.util.ArrayList; ??
  4. import?java.util.HashMap; ??
  5. import?java.util.List; ??
  6. import?java.util.Map; ??
  7. ??
  8. import?struts2jsonjquery.test.entity.UserInfo; ??
  9. ??
  10. import?com.opensymphony.xwork2.ActionSupport; ??
  11. ??
  12. public?class?JsonJqueryStruts2Action?extends?ActionSupport?{ ??
  13. ??
  14. ????private?static?final?long?serialVersionUID?=?3518833679938898354L; ??
  15. ???? ??
  16. ????private?String?message;???