easyui_05.DataGrid数据新增

6 篇文章 0 订阅

1.前端界面的开发

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/common/head.jsp"%>
<title>图书</title>
<script type="text/javascript">
	$(function(){
		$('#dg').datagrid({    
		    url:ctx+'/bookServlet',
		    toolbar: '#tb',
		    pagination:true,
		    singleSelect:true,
		    columns:[[    
		        {field:'id',title:'代码',width:100},    
		        {field:'book_name',title:'书本名称',width:100},    
		        {field:'book_price',title:'书本价格',width:100,align:'right'},  
		        {field:'book_type',title:'书本类型',width:100}
		    ]]    
		});  
		$("#qrybtn").click(function(){
			qry();
		});
		function qry(){
			$('#dg').datagrid("reload",{
				bookName:$("#bookName").val()
			});
		}
		$("#addbook").click(function(){
			$('#editDialog').dialog({    
			    title: '简简单单来个增加',    
			    width: 300,    
			    height: 260,    
			    closed: false,    
			    cache: false,    
			    href: 'editBook.jsp',    
			    modal: true,
			    buttons:[{
					text:'保存',
					handler:function(){
						$.ajax({
							url:ctx+"/bookAddServlet",
							type:'post',
							data:$("#bookForm").serialize(),
							dataType:'json',
							success:function(resp){
								if(resp.success){
									$.messager.alert('通知','操作成功');  
									$("#editDialog").dialog("close");
									qry();
								}else{
									$.messager.alert('警告','出现异常请联系管理员');  
								}
							}
						});						
					}
				},{
					text:'关闭',
					handler:function(){alert('adfa')}
				}]
			});
		});
	});
</script>
</head>
<body>
	<form action="bookServlet" method="post">
		<label for="name">书名:</label>   
		<input id="bookName" class="easyui-validatebox" type="text" name="name" data-options="required:true" /> 
		<a id="qrybtn" href="#" class="easyui-linkbutton" >查询</a>    
	</form>
	<div style="margin-top:10px;">
		<table id="dg"></table>  
	</div>
	<div id="tb" style="text-align:right;">
		<a href="#" id="addbook" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"></a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"></a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"></a>
	</div>
	<div id="editDialog">Dialog Content.</div> 
</body>
</html>

2.小窗口代码开发

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form id="bookForm" action="">
		<div style="margin-top:10px;margin-left:10px">
			 <label for="name">书本名称:</label>   
		     <input class="easyui-textbox" type="text" name="book_name" data-options="required:true" /> 
		</div>
		<div style="margin-top:10px;margin-left:10px">
			 <label for="name">书本价格:</label>   
		     <input class="easyui-textbox" type="text" name="book_price" data-options="required:true" /> 
		</div>
		<div style="margin-top:10px;margin-left:10px">
			 <label for="name">书本类型:</label>   
		     <input class="easyui-textbox" type="text" name="book_type" data-options="required:true" /> 
		</div>
	</form>
</body>
</html>

3.dao层和servie的代码开发

dao层

void addBook(Book book);
@Override
	public void addBook(Book book) {
		Connection con=null;
		PreparedStatement ps=null;
		try {
			String sql="insert into t_book(id,bookname,price,booktype) "
					+ " select max(id)+1,'"+book.getBook_name()+"',"+book.getBook_price()+",'"+book.getBook_type()+"' from t_book";
			con=DBHerpr.getCon();
			ps=con.prepareStatement(sql);
			int n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHerpr.myClose(con, ps, null);
		}
	}

servie层

	void addBook(Book book);
@Override
	public void addBook(Book book) {
		dao.addBook(book);
	}

4.实现类的代码开发

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import model.Book;
import servie.Bookservie;
import servie.IBookservie;
@WebServlet("/bookServlet")
public class Bookservlet extends HttpServlet{
	
	private IBookservie bk=new Bookservie();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=UTF-8");
		
		String name=req.getParameter("bookName");
		String pageindex=req.getParameter("page");
		int pid=pageindex==null ||"".equals(pageindex) ?1:Integer.parseInt(pageindex);
		int pageSize=10;
		List<Book> books=bk.getBook(name, pid, pageSize);
		int total=bk.getTotalPage(name);
		
		Map<String, Object> date=new HashMap<>();
		date.put("total", total);
		date.put("rows", books);
		
		String json=JSON.toJSONString(date);
		PrintWriter out= resp.getWriter();
		out.write(json);
		out.flush();
		out.close();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃亏了的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值