Struts2 + jQuery 实现ajax
使用这个组合,感觉还是很方便灵活的。
1、将struts2的json插件加入web工程的lib,jsonplugin的下载地址: http://code.google.com/p/jsonplugin/downloads/list
2、struts.xml添加专为ajax使用的package
<
package
name
="ajax"
extends
="json-default"
>
< action name ="ajaxRequest"
class ="org.david.struts2.HelloWorld" >
< result type ="json" ></ result >
</ action >
</ package >
< action name ="ajaxRequest"
class ="org.david.struts2.HelloWorld" >
< result type ="json" ></ result >
</ action >
</ package >
3、helloworld.jsp
<
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();" >
< 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();" >
4、HelloWorld.java
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";
}
}
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";
}
}