Struts2整合EasyUI DataGrid传递JSON数据【要点及常见错误】

Struts2整合EasyUI DataGrid传递JSON数据【要点及常见错误】

本人使用了IntelliJ IDEA2016 进行测试,期间遇到了不少问题,将在下面一一讲解。本文仅仅讲解struts2与EasyUI DataGrid前台的数据传输, 认为您会使用DataGrid

jar包

这是一个大坑,一定要注意,各种依赖的jar包如果版本不对的话,会遇到难以查找的Bug。涉及到的jar包,都可以在Struts2的发行包里找到。

推荐使用最新的、同一整合包里的jar文件。

我使用了如下版本jar包:


struts 2.5.10核心包

  1. commons-fileupload-1.3.2.jar
  2. commons-io-2.4.jar
  3. commons-lang3-3.4.jar
  4. freemarker-2.3.23.jar
  5. javassist-3.20.0-GA.jar
  6. log4j-api-2.7.jar
  7. ognl-3.1.12.jar
  8. struts2-core-2.5.10.jar

JSON lib

  1. commons-beanutils-1.9.2.jar
  2. commons-collections-3.2.1.jar
  3. commons-lang-2.4.jar
  4. commons-logging-1.1.3.jar’
  5. ezmorph-1.0.6.jar
  6. json-lib-2.3-jdk15.jar

Struts2 JSON plugin

  1. 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包版本造成的。

各个步骤会遇到的错误,都在上面一一指出,如有其它疑问,欢迎留言!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值