Struts2整合EasyUI DataGrid传递JSON数据【要点及常见错误】
本人使用了IntelliJ IDEA2016 进行测试,期间遇到了不少问题,将在下面一一讲解。本文仅仅讲解struts2与EasyUI DataGrid前台的数据传输, 认为您会使用DataGrid。
jar包
这是一个大坑,一定要注意,各种依赖的jar包如果版本不对的话,会遇到难以查找的Bug。涉及到的jar包,都可以在Struts2的发行包里找到。
推荐使用最新的、同一整合包里的jar文件。
我使用了如下版本jar包:
struts 2.5.10核心包
- commons-fileupload-1.3.2.jar
- commons-io-2.4.jar
- commons-lang3-3.4.jar
- freemarker-2.3.23.jar
- javassist-3.20.0-GA.jar
- log4j-api-2.7.jar
- ognl-3.1.12.jar
- struts2-core-2.5.10.jar
JSON lib
- commons-beanutils-1.9.2.jar
- commons-collections-3.2.1.jar
- commons-lang-2.4.jar
- commons-logging-1.1.3.jar’
- ezmorph-1.0.6.jar
- json-lib-2.3-jdk15.jar
Struts2 JSON plugin
- struts2-json-plugin-2.5.10.jar
如果以上JSON lib 和 plugin 版本过低,或者依赖包版本不匹配的时候,将会造成后台可以产生json串,而传递到前台时,产生对象为空的情况
后台控制台打印出json串:
{"total":2,"rows":[{"phone":"123456","name":"myth"},{"phone":"654321","name":"myth2"}]}
而传递到前端会产生:
{"array":false,"empty":false,"null Object":false}
您可以去官网下载all-in-one struts2包http://struts.apache.org/download.cgi,也可以下载我整理好的jar包http://download.csdn.net/detail/caipengbo/9753227。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
前端index.jsp
<%--
Created by IntelliJ IDEA.
User: Myth
Date: 2/12/2017
Time: 10:59 AM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>DataGrid</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/style/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/style/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.easyui.min.js"></script>
</head>
<body>
<table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
url="${pageContext.request.contextPath }/ajax.action"
toolbar="#toolbar"
rownumbers="true"
fitColumns="true"
singleSelect="true">
<thead>
<tr>
<th field="name" width="50">Name</th>
<th field="phone" width="50">Phone</th>
</tr>
</thead>
</table>
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
</div>
</body>
</html>
url请求为ajax.action
配置文件struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.locale" value="zh_CN" />
<package name="p1" extends="json-default"> <!--导入struts2-json-plugin.jar -->
<action name="ajax" class="cn.caipengbo.ajax.AjaxAction" method="doPrint">
<result type="json">
<param name="root">jsonresult</param> <!--很重要-->
</result>
</action>
</package>
</struts>
IntelliJ IDEA的目录结构和Eclipse不一样,lib与web属于同一级别,在WEB—INF下没有lib,最好将jar包放到WEB—INF下的lib下,这样可以避免一些部署时出现的jar包无法部署的问题。
如果无法继承json-default,就是因为jar导入的路径有问题,因为在struts2-json-plugin.jar里面定义了json-default,您可以自行百度解决方法,网上有很多。
动作类 AjaxAction.java
package cn.caipengbo.ajax;
import com.opensymphony.xwork2.Action;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Myth on 2/12/2017.
*/
public class AjaxAction implements Action {
private JSONObject jsonresult;
public JSONObject getJsonresult() {
return jsonresult;
}
public void setJsonresult(JSONObject jsonresult) {
this.jsonresult = jsonresult;
}
public String doPrint() throws Exception {
System.out.println("action execute");
List list = new ArrayList<>();
Map<String,Object> map = new HashMap<String,Object>();
Map<String,Object> map2 = new HashMap<String,Object>();
map.put("name","myth");
map.put("phone","123456");
map2.put("name","myth2");
map2.put("phone","654321");
list.add(map);
list.add(map2);
Map<String,Object> jsontemp = new HashMap<String,Object>();
jsontemp.put("total",list.size());
jsontemp.put("rows", list);
jsonresult = JSONObject.fromObject(jsontemp);
System.out.println(jsonresult);
return SUCCESS;
}
@Override
public String execute() throws Exception {
return null;
}
}
常见问题
步骤就是上述步骤,主要的问题,就是jar包版本造成的。
各个步骤会遇到的错误,都在上面一一指出,如有其它疑问,欢迎留言!!