日期:2014-05-16 浏览次数:20575 次
<package name="ajax" extends="json-default"> <action name="ajaxRequest" class="org.david.struts2.HelloWorld"> <result type="json"></result> </action> </package>
<SCRIPT type="text/javascript" src="js/jquery-1.2.6.min.js"></script> <SCRIPT type="text/javascript"> function clickButton() { var url = 'ajaxRequest.action'; var params = { name:$('#name').attr('value') }; jQuery.post(url, params, callbackFun, 'json'); } function callbackFun(data) { alert(data.result);//对应HelloWorld类的message属性 //获取数据后渲染页面 } </SCRIPT> <input id="name" type="text"> <input type="button" value="ok" onclick="javascript:clickButton();">
package org.david.struts2; public class HelloWorld { private String name; private String result; // ajax请求参数赋值 public void setName(String name) { this.name = name; } // ajax返回结果 public String getResult() { return result; } public String execute() { this.result = "Hello! " + this.name + "."; return "success"; } }