使用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中的

[html]  view plain copy
  1. <global-exception-mappings>  
  2.     <exception-mapping exception="java.lang.Exception"  
  3.         result="error" />  
  4. </global-exception-mappings>   
在开发的时候最好去掉,一个commons-lang的包忘记导入之后报了异常跳转到error页面让我根本就不知道哪里出错或者报异常了。


其实官方文档有简单的例子:

https://struts.apache.org/docs/json-plugin.html

首先是AjaxAction.java

[java]  view plain copy
  1. package action;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import net.sf.json.JSONObject;  
  7.   
  8. import org.apache.struts2.ServletActionContext;  
  9. import com.opensymphony.xwork2.Action;  
  10.   
  11. public class AjaxAction implements Action{  
  12.     private String jsonresult;  
  13.     public String getJsonresult() {  
  14.         return jsonresult;  
  15.     }  
  16.     public void setJsonresult(String jsonresult) {  
  17.         this.jsonresult = jsonresult;  
  18.     }  
  19.     public String json() throws Exception {  
  20.         System.out.println("action execute");  
  21.         Map<String,String> map = new HashMap<String,String>();  
  22.         map.put("b","b");  
  23.         System.out.println(map);  
  24.         JSONObject json  = null;  
  25.         json = JSONObject.fromObject(map);  
  26.         System.out.println("json");  
  27.         jsonresult = json.toString();  
  28.         System.out.println(jsonresult);  
  29.         return SUCCESS;  
  30.     }  
  31.     @Override  
  32.     public String execute() throws Exception {  
  33.         return null;  
  34.     }  
  35. }  

struts.xml中需要修改一些东西。原来的struts-default改为json-default

[html]  view plain copy
  1. <package name="default" namespace="/" extends="json-default">  

result 需要声明json的type。

[html]  view plain copy
  1. <action name="ajaxaction" class="action.AjaxAction" method="json">  
  2.         <result type="json"></result>  
  3.         <result name="hello">/hello.jsp</result>  
  4. </action>  

输出的结果

原理是怎样的呢,再改一下AjaxAction。

[java]  view plain copy
  1. package action;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import net.sf.json.JSONObject;  
  7.   
  8. import org.apache.struts2.ServletActionContext;  
  9. import com.opensymphony.xwork2.Action;  
  10.   
  11. public class AjaxAction implements Action{  
  12.     private String jsonresult;  
  13.     private String jsonsecond;  
  14.     private String jsonthird;  
  15.       
  16.     public String json() throws Exception {  
  17.         System.out.println("action execute");  
  18.         Map<String,String> map = new HashMap<String,String>();  
  19.         map.put("b","b");  
  20.         System.out.println(map);  
  21.         JSONObject json  = null;  
  22.         json = JSONObject.fromObject(map);  
  23.         System.out.println("json");  
  24.         jsonresult = json.toString();  
  25.         System.out.println(jsonresult);  
  26.         return SUCCESS;  
  27.     }  
  28.     @Override  
  29.     public String execute() throws Exception {  
  30.         return null;  
  31.     }  
  32.       
  33.     public String getJsonsecond() {  
  34.         return jsonsecond;  
  35.     }  
  36.     public void setJsonsecond(String jsonsecond) {  
  37.         this.jsonsecond = jsonsecond;  
  38.     }  
  39.     public String getJsonresult() {  
  40.         return jsonresult;  
  41.     }  
  42.     public void setJsonresult(String jsonresult) {  
  43.         this.jsonresult = jsonresult;  
  44.     }  
  45. }  
先看输出结果:


jsonsecond是null,jsonresult是json格式,竟然这两个都有,jsonthird为什么没有,因为没有相对应的get和set方法结果,jsonsecond是null是因为没有赋值。

struts就这样帮你将声明了的并且具有get、set方法的属性帮你输出了。

JSONObject帮你将map对象转化成json串。

为什么转成json串,因为有些前端需求特定的数据格式进行交互,例如后面写到的EasyUI。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值