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>
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();">
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"; } }
相关推荐
TONIYH 2020-07-22
83510998 2020-07-18
wcqwcq 2020-06-26
delmarks 2020-06-14
ppsurcao 2020-06-14
tthappyer 2020-06-07
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03