使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——ajax,struts2使用json格式的交互

继上篇:《使用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中的

<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数据交互的问题了。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iaiti

赏顿早餐钱~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值