日期:2014-05-16  浏览次数:20674 次

struts2使用ajax

??? 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>

??? ....

?六.感谢网友提供大量学习资料.

?

1 楼 guoyangjie 2011-04-13  
怎么没有JAR文件啊,可不可以上传上来
2 楼 xiaojianhx 2011-04-13  
guoyangjie 写道
怎么没有JAR文件啊,可不可以上传上来

好久了,哪天整理下发给你
3 楼 guoyangjie 2011-04-14  
谢谢了,我自己去官方下载了下