Struts2+ajax+json整合简介
2014-02-06 15:33
323 查看
一、准备好jar包,不要小看这一步,万事开头难。。总结起来有以下三种搭配。
1. xwork-core-2.1.6.jar和struts2-json-plugin-2.1.8.jar。如果你想使用struts2-json-plugin-2.1.8.jar这种支持方式,你的xwork-core-*.jar不能选择2.2.1及以上版本,因为xwork-core-*.jar的2.2.1及以上版本中没有了org.apache.commons.lang等包。启动tomcat的时候会出现:java.lang.NoClassDefFoundError:
org.apache.commons.lang.xwork.StringUtils。
2. xwork-2.1.2.jar和jsonplugin-0.34.jar。如果想用jsonplugin-0.34.jar这种支持方式,那需要切换你的xwork-core-*.jar为xwork-2.1.2.jar。因为jsonplugin-0.34.jar需要com.opensymphony.xwork2.util.TextUtils
这个类的支持。而xwork-core-*.jar的2.2.1以上版本均为找到该类,且在xwork-core-2.1.6.jar中也没有该类。
3 .struts2.3.7的版本,适合struts2-json-plugin-2.3.4.1.jar与json-lib-2.3-jdk15.jar
二、下面看下action是啥吧。
很简单,是个编辑问题的方法,笔者正在做一个互动问答平台,用户在提问之后,还可以编辑自己的问题。
上面就是实现这个功能的action代码。可以注意到,这个action里面有很多属性,struts.xml可以这样写。
includeProperties这个参数的配置对应的是下面这段ajax提交代码中的success回调函数中的data,如果data需要更多的内容,则可以在includeProperties参数里面添加更多的属性。 这个参数的配置很关键,配置错了,success回调函数就不会执行了,所以一定要注意!
三、再来看下ajax函数是怎么写的吧
这样,整合就算初具规模了,可以实现在struts2下,页面无刷新的表单提交了~
1. xwork-core-2.1.6.jar和struts2-json-plugin-2.1.8.jar。如果你想使用struts2-json-plugin-2.1.8.jar这种支持方式,你的xwork-core-*.jar不能选择2.2.1及以上版本,因为xwork-core-*.jar的2.2.1及以上版本中没有了org.apache.commons.lang等包。启动tomcat的时候会出现:java.lang.NoClassDefFoundError:
org.apache.commons.lang.xwork.StringUtils。
2. xwork-2.1.2.jar和jsonplugin-0.34.jar。如果想用jsonplugin-0.34.jar这种支持方式,那需要切换你的xwork-core-*.jar为xwork-2.1.2.jar。因为jsonplugin-0.34.jar需要com.opensymphony.xwork2.util.TextUtils
这个类的支持。而xwork-core-*.jar的2.2.1以上版本均为找到该类,且在xwork-core-2.1.6.jar中也没有该类。
3 .struts2.3.7的版本,适合struts2-json-plugin-2.3.4.1.jar与json-lib-2.3-jdk15.jar
二、下面看下action是啥吧。
public String editProblem(){ question=(Question) this.getSession().get("QUESTION_INFO"); this.pid=String.valueOf(question.getId()); question.setContent(this.newContent); question.setModifyDateTime(this.getCurrentDate()); showProblemsLogic.updateProblem(question);//更新数据库记录 return SUCCESS; }
很简单,是个编辑问题的方法,笔者正在做一个互动问答平台,用户在提问之后,还可以编辑自己的问题。
上面就是实现这个功能的action代码。可以注意到,这个action里面有很多属性,struts.xml可以这样写。
<action name="editProblem" class="problemsAction" method="editProblem"> <result name="input">/problems.jsp</result> <result type="json"> <param name="includeProperties">newContent</param> </result> </action>
includeProperties这个参数的配置对应的是下面这段ajax提交代码中的success回调函数中的data,如果data需要更多的内容,则可以在includeProperties参数里面添加更多的属性。 这个参数的配置很关键,配置错了,success回调函数就不会执行了,所以一定要注意!
三、再来看下ajax函数是怎么写的吧
<script> $(document).ready(function(){ $("#edit_content_button").click(function() { var params = $("#edit_form").serialize();//讲指定表单中的参数转换为查询字符串 //使用jQuery中的$.ajax({});Ajax方法 $.ajax({ url:"editProblem",//这个是对应的action名称 type:"POST", data:params, dataType:"json", success:function(data){ ajax请求提交成功后的回调函数 $("#qcontent").css("display","none"); $("#newqContent").empty(); $("#newqContent").append(data.newContent); $('#edit_form').toggle(); alert("修改成功!"); }, error:function(data){ alert(arguments[1]); } }); }); }); </script>
这样,整合就算初具规模了,可以实现在struts2下,页面无刷新的表单提交了~
相关文章推荐
- Struts+Spring+Hibernate项目整合AJAX+JSON
- ASP.NET MVC AJAX实现 以及如何处理json数据,简介
- struts1、ajax、jquery、json简单实例
- ssh整合问题总结--使用struts2+Ajax+jquery验证用户名是否已被注册
- Struts学习_Jquery发送ajax请求以及datatype参数为text/JSON方式
- 在Struts 2中使用JSON Ajax支持
- 在Struts 2中使用JSON Ajax支持
- Struts Ajax Json
- struts1与struts2整合ajax实现异步调用(服务器端实现)
- struts2+json整合的各种问题
- struts2.3.16 页面AJAX 请求 返回 JSON字符串
- Struts2+JSON 实现AJAX 返回对象和列表
- AJAX Struts 框架整合例子请求参数作为XML传送(-)
- Struts 2中启用json ajax支持
- (更新版)ExtJS Tree利用json(直接传List TreeNode,不需要转化为JSONArray)在Struts 2实现Ajax动态加载树结点
- struts2之使用JSON插件实现Ajax(在struts.xml中处理要序列化的属性)
- Struts2+ajax+json整合简介
- Struts+AJAX+JSON配合使用,获取与返回结果
- Ajax的工具类AjaxUtils,使用struts返回Json类型
- json +struts2+ajax