Struts2 ajax
来源:互联网 发布:cooledit有mac版 编辑:程序博客网 时间:2024/06/03 13:39
使用servlet api返回数据实现ajax
jsp:
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" > $(function(){ $('#btn').click(function(){ $.post("ajax.action",function(data){ $('#msg').html(data);});});});</script> </head> <body> <input type="button" id="btn" value="获取ajax信息"/> <h3 id="msg"></h3> </body></html>
action:
public class AjaxAction { public String execute() throws IOException{ HttpServletResponse resp=ServletActionContext.getResponse(); resp.setCharacterEncoding("utf-8"); resp.getWriter().print("ajax"); return null; }}
struts.xml
<struts> <package name="default" namespace="/" extends="list-default"> <action name="ajax" class="cn.sxt.action.AjaxAction"> </action> </package></struts>
使用struts2插件来实现ajax处理(返回json数据格式)
观察里面的xml,所以extends要继承的是json-default了
Action:
public class JsonAction { private JSONArray root; public String execute(){ List<User> list=new ArrayList<User>(); list.add(new User("s", 1)); list.add(new User("i",2)); list.add(new User("g", 3)); root=JSONArray.fromObject("list"); System.out.println("root="+root.toString()); return "success"; } public JSONArray getRoot() { return root; } public void setRoot(JSONArray root) { this.root = root; }}
struts.xml
<struts> <package name="default" namespace="/" extends="json-default"> <action name="json" class="cn.sxt.action.JsonAction"> <result type="json"> <param name="root">root</param> </result> </action> </package></struts>
jsp
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" > $(function(){ $('#btn').click(function(){ $.post("json.action",function(data){ var html=""; for(var i=0;i<data.length;i++){ html+="<tr><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>"; $('#content').html(html); }},'json');});});</script> </head> <body> <input type="button" id="btn" value="获取ajax信息"/> <table width="80%" > <tr> <td>姓名</td> <td>年龄</td> </tr> <tbody id="content"> </tbody> </table> </body></html>
阅读全文
0 0
- Struts2 ajax
- struts2 + ajax
- struts2+ajax
- Struts2&&AJAX
- struts2 ajax
- struts2 + ajax
- ajax+struts2
- struts2 ajax
- struts2 ajax
- Struts2 AJAX
- struts2 ajax
- Struts2 ajax
- struts2+spring+jpa+ajax
- jQuery+Ajax+Struts2.0
- Struts2 AJAX 验证
- struts2+ajax的应用
- ajax struts2 validation
- Struts2的ajax支持
- golang的Channel
- 一篇文章
- html学习8—目录—有序列表
- HDU_1556_树状数组
- Head First学习笔记(2)
- Struts2 ajax
- Active Object and sample code in symbian
- 开启讲师之路
- iOS开发之cocoapods出现Diff: /../Podfile.lock: No such file or directory错误
- Androud7.0之IPC机制(多进程、及其通信)
- electron 安装使用心酸入门随笔
- 浅析 Http Header 功能
- noip模拟 马云 Mr_H
- Hadoop2和Hadoop1的区别