http://www.blogjava.net/ITdavid/archive/2008/09/07/227612.html
、将struts2的json插件加入web工程的lib,jsonplugin的下载地址:http://code.google.com/p/jsonplugin/downloads/list
2、struts.xml添加专为ajax使用的package
<package name="example" extends="json-default">
<action name="ajaxRequest"
class="com.srj.action.temp.HelloWorld">
<result type="json"></result>
</action>
</package>
3、helloworld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<head>
<SCRIPT type="text/javascript" src="css/jquery-1.9.1.min.js"></script>
</head>
<body>
<SCRIPT type="text/javascript">
function clickButton()
{
var url = 'ajaxRequest.action';
var params = {
//name:$('#name').attr('value')
name:$('#name').val()
};
jQuery.post(url, params, callbackFun, 'json');
}
function callbackFun(data)
{
//alert(data.result);//对应HelloWorld类的message属性
//获取数据后渲染页面
$("#showdiv").html("你好"+data.result+"</br>");
}
</SCRIPT>
<input name="name" id="name" type="text">
<input type="button" value="ok" οnclick="javascript:clickButton();">
<div id="showdiv">
</div>
</body>
</html>
4、HelloWorld.java
package com.srj.action.temp;
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";
}
}