strust2中jquery实现ajax
来源:互联网 发布:心目中最好av作品知乎 编辑:程序博客网 时间:2024/05/23 13:43
最近在项目中需要使用ajax,根据网上的资料自己写了一个测试小程序,现将代码奉上。一些需要注意的地方我都加在注释里了。
配置struts的东西我就不说了,网上有很多,自己去看吧。这个是在已经会配置struts的基础上才写的。
Action类
package action;import java.util.ArrayList;import java.util.List;import org.apache.struts2.json.annotations.JSON;import com.opensymphony.xwork2.ActionSupport;import entity.User;/*步骤1 加入struts2-json-plugin-2.3.4.jar插件 * 步骤2 struts.xml文件中 <package name="default" namespace="/" extends="json-default"> *extends必须这样写 * 步骤3 在struts.xml配置action的时候result写成:<result name="success" type="json"></result> * * 将可以返回普通数据,返回对象,返回List * 定义了一个Action里面所有的get方法并且没有使用@JSON(serialize = false) * 注解的数据都将会被返回 * set方法在这里可以去掉,如果你不需要从前台jsp中传值给这个Action对应的get方法都可以去掉 */public class AjaxAction extends ActionSupport {private String name;private User user;private List<User> list;public String check() {this.name = "ddddd";this.user = new User();user.setName("AAAAAAAAAA");user.setSex("man");list = new ArrayList<User>();list.add(user);this.user = new User();user.setName("BBBBBBBBBBBB");user.setSex("woman");list.add(user);return this.SUCCESS;}//加上此注解重要的是设置序列化为false,返回的数据中将不会返回此数据@JSON(serialize = false)public String getName() {return name;}public void setName(String name) {this.name = name;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public List<User> getList() {return list;}public void setList(List<User> list) {this.list = list;}}
package entity;public class User {private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}
struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="json-default"><action name="check" class="action.AjaxAction" method="check"><result name="success" type="json"></result></action> </package></struts>
前台index.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript" src="js/jquery-1.8.1.min.js"></script><script type="text/javascript">function loadInfo(){//远程调用check方法,不传参数null决定,定义了回调函数参数data是返回的数据//最后定义了返回的是json数据,jquery会自动将返回的数据进行json解析,此参数还可以是text、xml等具体参见文档$.post("check",null,function(data){//直接使用数据,这里不能取得data.name的值,因为在Action中使用注解使其不被返回alert(data.name+","+data.user.name+","+data.user.sex);//对于List的循环$.each(data.list,function(i,item){alert(item.name+","+item.sex);});},"json");}</script> </head> <body> This is my JSP page. <br> <input type="button" value="获取" id="btnGet" onclick="loadInfo()" /> <div id="info"></div </body></html>
读者看的时候如果准备自己运行,必须将对应的jquery文件修改。想要源码的可以直接给我发邮件598660766@qq.com
- strust2中jquery实现ajax
- strust2 jquery ajax
- jquery中ajax的实现
- Strust2第(十四)篇《struts2+ajax实现异步验证》
- Strust2中的AJAX
- AJAX-jQuery实现Ajax
- 在JQuery中实现Ajax编程
- .net MVC中Jquery实现AJAX详解
- 《Strust2》-实现照片上传
- Strust2实现文件上传
- strust2 中Tiles使用
- jQuery中Ajax-ajax方法
- JQuery中使用ajax $.ajax()
- jquery实现Ajax
- jQuery实现AJAX插件
- jquery 实现ajax
- jquery实现ajax
- jQuery实现Ajax
- openssl gendsa 生成DSA私钥
- Convolution Reverb
- android-数字提醒,类似邮箱的未读邮件功能
- PHP配合apc实现上传进度条
- DES
- strust2中jquery实现ajax
- java.util.concurrent介绍
- C++的XML编程经验――LIBXML2库使用指南
- 基于AIR实现的Android程序
- ios6 支持旋转问题
- 事务的原子性,一致性,隔离性,持久性
- ErgodicFile
- python应用
- 软件测试