Easyui_4_datagrid之查询

9 篇文章 0 订阅
6 篇文章 0 订阅

一、代码

 userManage.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/icon.css">   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>  
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/book.js"></script> 
<title>存放书籍页面</title>
</head>
<body>
<input type="hidden" id="ctx" value="${pageContext.request.contextPath }">
 
<!-- <div id="tb">
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"/a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-help',plain:true"/a>
</div> -->

<div id="tb">
	<input class="easyui-textbox" id= "name" name="name" style="width:30%; padding-left: 10px" data-options="label:'书名:',required:true">
	<a id="btn-search" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
	<!-- <a id="" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">新增</a> -->
	

</div>





<table id="dg"></table> 
</body>
</html>

book.js

$(function(){
	/*在easyUI中,点击上一页下一页等默认的范爷效果,携带的参数是page/rows
	在bootstrap中,点击上一页下一页等默认的范爷效果,携带的参数是page/offset*/
	$('#dg').datagrid({    
	    url:$("#ctx").val()+'/book.action?methodName=datagrid',   
	    pagination:true,
	    fitColumns:true,
	    toolbar: '#tb',
	    columns:[[    
	        {field:'bid',title:'id',width:100},    
	        {field:'bname',title:'名称',width:400},    
	        {field:'price',title:'价格',width:100,align:'right'}    
	    ]]    
	});  
	
	$("#btn-search").click(function(){
		$('#dg').datagrid('load', {    
			bname: $("#name").val()   
	    
		});  
	
	});
})

book实体

package com.dzl.entity;

public class Book {
	
	private int bid;
	private String bname;
	private float price;
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
	}
	
	
}

 BookDao

package com.dzl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dzl.entity.Book;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;

public class BookDao extends BaseDao<Book> {
/*ctrl+shift+o 导包*/
		
//	查询
	public List<Book> list(Book book, PageBean pageBean) throws Exception {
		String bname = book.getBname();
		String sql ="select * from t_mvc_book where 1=1";
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+ bname +"%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}
	
	public static void main (String [] args) throws Exception {
		BookDao bookDao = new BookDao();
		PageBean pageBean=new PageBean();
		List<Book> list = bookDao.list(new Book(),new PageBean());
		ObjectMapper om = new ObjectMapper();
//		json数组
		Map<String,Object>map =new HashMap<String, Object>();
		map.put("total", pageBean.getTotal());
		map.put("rows", list);
		System.out.println(om.writeValueAsString(map));
	}




	
	
}

BookAction

package com.dzl.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dzl.dao.BookDao;
import com.dzl.entity.Book;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.PageBean;
import com.zking.util.R;
import com.zking.util.ResponseUtil;

public class BookAction extends ActionSupport implements ModelDriver<Book> {
	 Book book = new Book();
     BookDao bookDao = new BookDao();
	
	@Override
	public Book getModel() {
		// TODO Auto-generated method stub
		return book;
	}
	
	public String datagrid(HttpServletRequest req, HttpServletResponse resp) {
		BookDao bookDao = new BookDao();
		PageBean pageBean=new PageBean();
		pageBean.setRequest(req);
		try {
			List<Book> list = bookDao.list(book, pageBean);
			ObjectMapper om = new ObjectMapper();
//		json数组		链式编程
//			Map<String,Object>map =new HashMap<String, Object>();
//			map.put("total", pageBean.getTotal());
//			map.put("rows", list);
			ResponseUtil.writeJson(resp,new R()
					.data("total",pageBean.getTotal())
					.data("rows",list));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	

}

配置mvc2.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	
<action type="com.zking.web.BlogAction" path="/blog">

<forward path="/blogList.jsp" redirect="false" name="list"/>

<forward path="/blog.action?methodName=list" redirect="true" name="toList"/>

<forward path="/blogEdit.jsp" redirect="false" name="toEdit"/>

</action>
	
	<action path="/menu" type="com.dzl.web.MenuAction">
	</action>
	
	<action path="/book" type="com.dzl.web.BookAction">
	
	</action>
</config>

二、链式编程

新建R:

package com.zking.util;

import java.util.HashMap;

public class R extends HashMap{
		public R data(String key,Object value) {
			this.put(key, value);
			return this;
		}

}

用:

	public String datagrid(HttpServletRequest req, HttpServletResponse resp) {
		BookDao bookDao = new BookDao();
		PageBean pageBean=new PageBean();
		pageBean.setRequest(req);
		try {
			List<Book> list = bookDao.list(book, pageBean);
			ObjectMapper om = new ObjectMapper();
//			链式编程
			ResponseUtil.writeJson(resp,new R()
					.data("total",pageBean.getTotal())
					.data("rows",list));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

执行效果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值