layui数据表格与java后台数据交互

layui数据表格与java后台数据交互

jsp部分


<table class="layui-hide" id="test"
lay-filter="test"></table>

js


layui.use('table', function(){

var table = layui.table;



table.render({

elem: '#test'

,url:'http://localhost:8080/news_ssh/newsAction_news.action'

,toolbar: '#toolbarDemo'

,title: '用户数据表'

,limit: 10

,cols: [[

{type: 'checkbox', fixed: 'left'}

,{field:'id', title:'序号', width:80, fixed: 'left', unresize: true, sort: true}

,{field:'title', title:'标题', width:100}

,{field:'createTime', title:'录入时间', sort: true}

,{field:'name', title:'栏目', width:80}

,{field:'author', title:'录入者', width:120}

,{field:'source', title:'来源', width:100}

,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}

]]

,page: true

});

layui的数据来自url,layui规定url返回的数据为json格式,这就需要把java的数据转为json格式,因为json-lib-2.4-jdk15要依赖以下jar包

commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.6.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar、xom-1.2.6.jar,比较麻烦,推荐用阿里的fastjson,fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理json格式数据的一个工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:

1).速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。包括自称最快的jackson。

2).功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。

3).无依赖,能够直接运行在Java SE 5.0以上版本

4).支持Android。

5).开源 (Apache 2.0)
我用的是fastjson-1.2.49.jar
fastJson的网址:http://code.alibabatech.com/wiki/display/FastJSON/Overview

NewsAction

public class NewsAction extends SuperAction implements ModelDriven<News> {

 public News n=new News();

 /*layui的数据表格向url发送请求时,会附上page以及limit这两个参数,
 所以服务端需要接收并以此为条件查找数据*/
 private int page=1;

 private int limit=10;

 public int getPage() {

  return page;

 }

 public void setPage(int
page) {

  this.page = page;

 }

 public int getLimit() {

  return limit;

 }

 public void setLimit(int
limit) {

  this.limit = limit;

 }

 public int TypeId=0;

 public int newsTypeId=0;

 public String news() throws Exception{

    WebApplicationContext applicationContext =
WebApplicationContextUtils

    .getWebApplicationContext(ServletActionContext.getServletContext());

  NewsDao bdao = (NewsDaoImpl)
applicationContext.getBean("newsDao");
/*queryPageNews方法的查询语句为hql="select News.id,News.title,News.createTime,nt.name,News.source,News.author from News News,NewsType nt where News.state=? and News.newsTypeId=nt.id "+
  "and News.del=0 order by News.createTime desc";因为Hibernate多表联查,所以手动将结果集转化成JSON*/
  List<Object[]>list=bdao.queryPageNews(this.getPage(), this.getLimit(), 0);

  List<NewsAndNewsType> mylist = new
LinkedList<NewsAndNewsType>();

  NewsAndNewsType nt;

  //System.out.println(list);

  for(Object[] object:list){             
  

   //System.out.println(object.length);           
 

   nt = new
NewsAndNewsType();              


   nt.setId(object[0].toString());

   nt.setTitle((String) object[1]);

   nt.setCreateTime(object[2].toString());

   nt.setName((String) object[3]);

   nt.setSource((String) object[4]);

   nt.setAuthor((String) object[5]);

   mylist.add(nt);             


   }  

  session.removeAttribute("news_list");

  //放进session中

  if(list!=null&&list.size()>0){

   session.setAttribute("news_list", list);

   Map<String, Object> result = new HashMap<String,
Object>();

      result.put("code", 0);

      result.put("msg", "");

     
result.put("count",bdao.getqueryPageNewsCount(0));

      JSONArray array = (JSONArray)
JSON.toJSON(mylist);

      result.put("data", array);

      // 将其转换为JSON数据,并压入值栈返回

     
ActionContext.getContext().getValueStack().set("jsonData",JSON.toJSON(result));

      return "success";

  }else{

   session.setAttribute("listSize",list.size());

   System.out.println("成功查询,查询条数"+list.size());

  }

  System.out.println("成功查询,查询条数"+list.size());

  return SUCCESS;

 }

 public News
getModel() {

  // TODO Auto-generated method stub

  return null;

 }

}

struts.xml配置

<package name="myNews" extends="json-default" namespace="/">
  <action name="newsAction_*" class="com.sise.web.NewsAction" method="{1}">
        <result name="success" type="json">
            <param name="root">jsonData</param>
        </result>
        </action>
 </package>

注意,struts.xml中extends="json-default"要导入jar包struts2-json-plugin-2.3.24.jar,param要为root
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值