继上篇:《使用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数据交互的问题了。