日期:2014-05-17 浏览次数:20837 次
public String execute() throws Exception {
HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
List<User> users=new ArrayList<User>();
User u1=new User("张三", 12);
User u2=new User("小美", 15);
users.add(u1);
users.add(u2);
JSONArray json=JSONArray.fromObject(users);
out.print(json.toString());
return super.SUCCESS;
<package name="default" namespace="/" extends="struts-default">
<!-- 配置测试的action -->
<action name="test" class="action.JsonForAction">
<result name="success">/jsonForAction.jsp</result>
</action>
<p id="wait"></p>
<input type="hidden" value="小明" id="name"/>
<input type="hidden" value="17" id="age"/>
<input type="button" value="显示" id="bnt"/>
<div id="display"></div>
$("#bnt").click(function(){
$.ajax({
type:"post", //请求方式
url:"http://localhost:8080/Ajax/test.action", //发送请求地址
data:{ //发送给数据库的数据
name:$("#name").val(),
age:$("#age").val()
},
//请求成功后的回调函数有两个参数
success:function(data,textStatus){
$("#wait").css("display","none");
var jsonObj = eval(data);
for(var i=0;i<jsonObj.length;i++){
$("#display").html($("#display").html()+jsonObj[i].name+"今年"+jsonObj[i].age+"岁!!!<br/>");
}
}
});
});