AJAX请求及json对象封装小demo

js中的ajax请求方法
function query(){
var stuName = $("#stuName").val();
var requestUrl = "/s2sh/student/query.action";
var currentPageIndex = $("span[class='currentPageIndex']").val();
var pageSize =$("input[class='pageSize']").val();
$.ajax( {
url :requestUrl,
type : 'post',
dataType:'json',
data: "stuName="+escape(escape(stuName))+"¤tPageIndex="+currentPageIndex+"&pageSize="+pageSize,
success : function(result){
var stus = result.students;
$("#stuBody").html("");
for(var i=0;i $("#stuBody").append(""+stus[i].stuName+""+stus[i].stuSex+""+
stus[i].stuBir+""+stus[i].stuAdd+" ">修改  删除");
}
$("span[class='totalCount']").html(result.totalCount);
$("span[class='currentPageIndex']").html(result.currentPage).val(result.currentPage);
$("span[class='totalPage']").html(result.totalPage).val(result.totalPage);
},error:function(err){
alert( '系统内部错误'+err);
}
});

}


Action的接受及封装
public class StudentAction {
public StudentService studentService;

private String stuName;

public String getStuName() {
return stuName;
}

public void setStuName(String stuName) {
this.stuName = stuName;
}


public StudentService getStudentService() {
return studentService;
}

public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}

public void query() {

HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();

String tempName = unescape(request.getParameter("stuName"));


try {


students=studentService.queryStuByName(tempName,currentPageIndex,pageSize);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(getStudentListJSON(students));


} catch (Exception e) {
e.printStackTrace();
}

}

public String getStudentListJSON(List students)
{
StringBuffer retVal=new StringBuffer("{'students':[");
int i=0;
for(Student t:students){
if(i!=0)
{
retVal.append(",");
}
retVal.append("{");
retVal.append("\"stuId\":\""+t.getStuId()+"\"");
retVal.append(",\"stuName\":\""+t.getStuName()+"\"");
retVal.append(",\"stuSex\":\""+t.getStuSex()+"\"");
retVal.append(",\"stuBir\":\""+t.getStuBir()+"\"");
retVal.append(",\"stuAdd\":\""+t.getStuAdd()+"\"");
retVal.append("}");
i=i+1;
}
retVal.append("],\"totalPage\":\""+10+ "\"}");
return retVal.toString();
}


@SuppressWarnings("unused")
private String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}

return tmp.toString();

}

}

你可能感兴趣的:(j2se学习笔记)