日期:2014-05-16 浏览次数:20674 次
??? struts版本:2.1.8.1
一.需要包(2个)
??? json-lib-2.1.jar
??? struts2-json-plugin-2.1.8.1.jar
二.下载js(两个)
??? json.js
??? prototype.js
三.配置文件中添加如下代码.
?
<struts> <package name="TestAjax" extends="json-default"> <action name="TestAjax" class="com.DreamAjaxAction"> <result type="json" /> </action> </package> </struts>
?四.创建DreamAjaxAction
package com; import com.opensymphony.xwork2.ActionSupport; public class DreamAjaxAction extends ActionSupport { private String result; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String execute() throws Exception { result = "ajax测试"; return SUCCESS; } }
?五.测试jsp
??? ...
??? <script src="../json.js" type="text/javascript"></script>
??? <script src="../prototype.js" type="text/javascript"></script>
??? <script>
??????? function setTest() {
?????????? var url = "TestAjax
.action";
?????????? new Ajax.Request(url,{method: 'post',onComplete: getTest});
??????? }
??????? function getTest(originalRequest){
??????????? document.getElementById("test").innerHTML=
?????????????????????????????????????????????????????? originalRequest.responseText.parseJSON().result;
??????? }
???? </script>
???? <input type="button" onclick="setTest()"/>
?? <span id="test"></span>
??? ....
?六.感谢网友提供大量学习资料.
?