使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——ajax,struts2使用json格式的交互
2015-09-19 18:31
856 查看
继上篇:《使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互》讲了这两者的交互之后,接下来要通过json的格式进行传输数据。
这里有3中方法:
1、像上篇在最后说的方法,特定内容只要先转换成json格式后,再发送到页面,其实不难。
2、用ServletActionContext.getResponse().getWriter().println(content),content的内容为json格式也可以,然后return Action.NONE这种方式也是可以的。
3、使用struts的json插件。
准备好struts2-json-plugin-2.3.16.3.jar包。
还有json格式的转换类JSONObject包以及所需要的jar包:
本身需要:
但是这部分和struts的包有重复去除掉重复部分即可。
struts.xml中的
其实官方文档有简单的例子:
https://struts.apache.org/docs/json-plugin.html
首先是AjaxAction.java
struts.xml中需要修改一些东西。原来的struts-default改为json-default
result 需要声明json的type。
输出的结果
原理是怎样的呢,再改一下AjaxAction。
先看输出结果:
jsonsecond是null,jsonresult是json格式,竟然这两个都有,jsonthird为什么没有,因为没有相对应的get和set方法结果,jsonsecond是null是因为没有赋值。
struts就这样帮你将声明了的并且具有get、set方法的属性帮你输出了。
JSONObject帮你将map对象转化成json串。
为什么转成json串,因为有些前端需求特定的数据格式进行交互,例如后面写到的EasyUI。
有了前面两篇的基础,使用一下struts自带的插件还有JSONObject类就解决了
ajax与struts间使用json数据交互的问题了。
这里有3中方法:
1、像上篇在最后说的方法,特定内容只要先转换成json格式后,再发送到页面,其实不难。
2、用ServletActionContext.getResponse().getWriter().println(content),content的内容为json格式也可以,然后return Action.NONE这种方式也是可以的。
3、使用struts的json插件。
准备好struts2-json-plugin-2.3.16.3.jar包。
还有json格式的转换类JSONObject包以及所需要的jar包:
本身需要:
但是这部分和struts的包有重复去除掉重复部分即可。
struts.xml中的
<global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error" /> </global-exception-mappings>在开发的时候最好去掉,一个commons-lang的包忘记导入之后报了异常跳转到error页面让我根本就不知道哪里出错或者报异常了。
其实官方文档有简单的例子:
https://struts.apache.org/docs/json-plugin.html
首先是AjaxAction.java
package action; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class AjaxAction implements Action{ private String jsonresult; public String getJsonresult() { return jsonresult; } public void setJsonresult(String jsonresult) { this.jsonresult = jsonresult; } public String json() throws Exception { System.out.println("action execute"); Map<String,String> map = new HashMap<String,String>(); map.put("b","b"); System.out.println(map); JSONObject json = null; json = JSONObject.fromObject(map); System.out.println("json"); jsonresult = json.toString(); System.out.println(jsonresult); return SUCCESS; } @Override public String execute() throws Exception { return null; } }
struts.xml中需要修改一些东西。原来的struts-default改为json-default
<package name="default" namespace="/" extends="json-default">
result 需要声明json的type。
<action name="ajaxaction" class="action.AjaxAction" method="json"> <result type="json"></result> <result name="hello">/hello.jsp</result> </action>
输出的结果
原理是怎样的呢,再改一下AjaxAction。
package action; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class AjaxAction implements Action{ private String jsonresult; private String jsonsecond; private String jsonthird; public String json() throws Exception { System.out.println("action execute"); Map<String,String> map = new HashMap<String,String>(); map.put("b","b"); System.out.println(map); JSONObject json = null; json = JSONObject.fromObject(map); System.out.println("json"); jsonresult = json.toString(); System.out.println(jsonresult); return SUCCESS; } @Override public String execute() throws Exception { return null; } public String getJsonsecond() { return jsonsecond; } public void setJsonsecond(String jsonsecond) { this.jsonsecond = jsonsecond; } public String getJsonresult() { return jsonresult; } public void setJsonresult(String jsonresult) { this.jsonresult = jsonresult; } }
先看输出结果:
jsonsecond是null,jsonresult是json格式,竟然这两个都有,jsonthird为什么没有,因为没有相对应的get和set方法结果,jsonsecond是null是因为没有赋值。
struts就这样帮你将声明了的并且具有get、set方法的属性帮你输出了。
JSONObject帮你将map对象转化成json串。
为什么转成json串,因为有些前端需求特定的数据格式进行交互,例如后面写到的EasyUI。
有了前面两篇的基础,使用一下struts自带的插件还有JSONObject类就解决了
ajax与struts间使用json数据交互的问题了。
相关文章推荐
- jquery Ajax的load、post、get、put、delete的用法
- “jquery于each方法和选择”学习笔记
- jquery closest()方法
- Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值
- jquery中的cookie操作
- jquery
- jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
- 一招解决!!!解决ECSHOP中transport.js和jquery的冲突
- jQuery Ajax 实例演示
- jQuery判断 form表单提交时一些文本框的判断
- jQuery attr("value") 和 val的区别
- 利用jquery给指定的table动态添加行、删除行
- jquery 的 promise编程
- 采用jquery的imgAreaSelect样品图像裁剪示范插件实现
- jquery插件datepicker
- jquery插件autocomplete
- jquery在火狐浏览中上传图片并显示
- jquery的验证实例方法
- 【.Net码农】JQuery上传插件Uploadify使用详解
- Java知识整理(二十四)之jQuery