日期:2014-05-16 浏览次数:20364 次
public class Person { private String name; private Date birthday; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getBirthday() { return birthday; } }
public class JSONResult { static public final int RETCODE_OK = 0; static public final int RETCODE_ERR = -1; private int returnCode; private String errMessage; public JSONResult(int returnCode) { this.returnCode = returnCode; } public JSONResult(int returnCode, String errMessage) { this.returnCode = returnCode; this.errMessage = errMessage; } public void setReturnCode(int returnCode) { this.returnCode = returnCode; } public int getReturnCode() { return returnCode; } public void setErrMessage(String errMessage) { this.errMessage = errMessage; } public String getErrMessage() { return errMessage; } }
function addButton_click() { //读取人员信息 var person = new Object(); person.name = document.getElementById("fullname").value; person.birthday = document.getElementById("birthday").value; //转换为JSON格式,并以"personData"做为属性名 var strPerson = YAHOO.lang.JSON.stringify(person); var postData = "personData=" + strPerson; //准备回调函数(相应处理函数见下文) var callback = { success :responseSuccess, failure :responseFailure, argument : null }; //AJAX异步调用 var request = YAHOO.util.Connect.asyncRequest('POST', 'RegisterPersonAction.action', callback, postData); }
function responseSuccess(o) { /* o.tId * o.status * o.statusText * o.getResponseHeader[ ] * o.getAllResponseHeaders * o.responseText * o.responseXML * o.argument */ //将返回的JSON串转成对象 var ret = YAHOO.lang.JSON.parse(o.responseText); if (ret.returnCode == 0) { //successed alert("成功"); } else { //failed alert("失败 :" + ret.errMessage); } }; function responseFailure(o) { alert("服务器处理失败 : " + o.statusText); }
<action name="RegisterPersonAction" method="register" class="personAction"> <result type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> </action>
public class PersonAction extends ActionSupport { private InputStream inputStream; public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public InputStream getInputStream() { return inputStream; } private PersonService personService; public void setPersonService(PersonService personService) { this.personService = personService; } public PersonService getPersonService() { return personService; } private String personData; public void setPersonData(String personData) { this.personData = personData; } public String getPersonData() { return personData; } public String register() { //将JSON字符串转成java的Person对象 JSONObject jsonPerson = JSONObjec