Struts2 (Json) + jQuery 组合的简单用法
1、将struts2的json插件加入web工程的lib,jsonplugin的下载地址:http://code.google.com/p/jsonplugin/downloads/list2、struts.xml添加专为ajax使用的package
<package name="ajax" extends="json-default"> <action name="ajaxRequest" class="org.david.strutsHelloWorld"> <result type="json"></result> </action> </package>
3、helloworld.jsp
<SCRIPT type="text/javascript" src="js/jquery-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"; } }
转自:http://hi.baidu.com/gukeming888/blog/item/c68ae3f9e345b40da9d311c0.html
相关推荐
hgzhang 2020-05-06
白净垃圾桶 2020-05-04
playis 2020-04-18
郭大路路 2020-04-16
lenchio 2020-04-08
yixiaoqi00 2020-04-08
melonjj 2020-01-05
mmywcoco 2019-12-28
wangruiling 2014-06-13
yaoyao0 2014-06-01
YangHuiLiang 2014-05-20
方志朋 2019-12-22
xcguoyu 2014-01-18
方志朋 2015-04-30
殷龙飞 2014-06-24
zmysna 2016-12-16
89264255 2011-04-23
wuddny的blog 2014-12-04