Struts2+JQuery+JSON实现异步交互,包括从后台获得单个值、对象、List和Map数据并从前台的表达中获取值把值进行序列化通过JQuery的$.ajax({})传到后台和后台的对象进行绑定。
第一步:在MyEclipse中创建JavaWeb工厂并把Struts2?和 json的jar包添加到工程中添加完成后的工程图是:
?
第二步:创建后台:
? 1. UserInfo实体类:
?
- 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; ??
- ????} ??
- }??
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类
- package?struts2jsonjquery.test.action; ??
- ??
- import?java.util.ArrayList; ??
- import?java.util.HashMap; ??
- import?java.util.List; ??
- import?java.util.Map; ??
- ??
- import?struts2jsonjquery.test.entity.UserInfo; ??
- ??
- import?com.opensymphony.xwork2.ActionSupport; ??
- ??
- public?class?JsonJqueryStruts2Action?extends?ActionSupport?{ ??
- ??
- ????private?static?final?long?serialVersionUID?=?3518833679938898354L; ??
- ???? ??
- ????private?String?message;???