页面向action中发送AJAX、json请求前面已经讲过了,这里不再重复,今天主要讲一下后台如何处理并返回json数据。
方式不唯一,会用其中一种就可以,我用的是Map
首先action中创建map对象
private Map result;//用于返回AJAX
public Map getResult() {
return result;
}
public void setResult(Map result) {
this.result = result;
}
在需要返回的处理方法中填充map,map中可以放入String,boolean,实体对象等。其中editEdutype为对象
map.put("result",true);
// map.put("edutypid",editEdutype.getEdutypid());
// map.put("edutypename", editEdutype.getEdutypename());
// map.put("edutypecharcter", editEdutype.getEdutypecharcter());
// map.put("edutypedesc", editEdutype.getEdutypedesc());
// map.put("status", editEdutype.getStatus());
map.put("editEdutype", editEdutype);
然后在structs.xml中配置
首先,package要继承json-default
<package name="default" namespace="/" extends="json-default">
然后配置action
<action name="user_*" class="newEmployeeAction" method="{1}">
<result name="ajaxlistSUCCESS" type="json">
<param name="root">result</param>
</result>
</action>
最后在jsp中调用返回的json
success: function(data) {
if(true == data.result){
var edutypID = data.edutypid
//alert(edutypID);
//document.getElementById("picUserID").value=userID;
alert("保存成功");
var url = "edutype_pagelist.action";
// alert("url为:" + url);
window.location.href=url;
} else {
alert("保存失败");
}