JqueryEasyUI实现CRUD增删改查操作

1.代码介绍:

前端界面由jsp,JqueryEasyUI制作,后台代码由Servlet实现逻辑操作

注:JqueryEasyUI的库文件和其他自己jar包自己导入。JqueryEasyUI的库文件下载地址:http://www.jeasyui.com/download/index.php

2.jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP ‘index.jsp‘ starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

    <link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
    <script type="text/javascript" src="easyui/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>

  </head>

  <script type="text/javascript">
          var url;
        function newUser(){
            $(‘#dlg‘).dialog(‘open‘).dialog(‘setTitle‘,‘新增‘);
            $(‘#fm‘).form(‘clear‘);
            url = ‘UserServlet?f=add‘;
        }
        function editUser(){
            var row = $(‘#mTb‘).datagrid(‘getSelected‘);
            if (row){
                $(‘#dlg‘).dialog(‘open‘).dialog(‘setTitle‘,‘修改‘);
                $(‘#fm‘).form(‘load‘,row);
                url = ‘UserServlet?f=update&id=‘ + row.id;
            }
        }
        function saveUser(){
            $(‘#fm‘).form(‘submit‘,{
                url: url,
                onSubmit: function(){
                    return $(this).form(‘validate‘);
                },
                success: function(result){
                    var result = eval(‘(‘+result+‘)‘);
                    if (result.success){
                        $.messager.show({
                            title: ‘提示‘,
                            msg: result.message
                        });
                        $(‘#dlg‘).dialog(‘close‘);        // close the dialog
                        $(‘#mTb‘).datagrid(‘reload‘);    // reload the user data
                    } else {
                        $.messager.show({
                            title: ‘提示‘,
                            msg: result.message
                        });
                    }
                }
            });
        }
        function removeUser(){
            var row = $(‘#mTb‘).datagrid(‘getSelected‘);
            if (row){
                $.messager.confirm(‘确认‘,‘您确定要删除吗?‘,function(r){
                    if (r){
                        $.post(‘UserServlet?f=delete‘,{id:row.id},function(result){
                            if (result.success){
                                $.messager.show({    // show error message
                                        title: ‘提示‘,
                                        msg: result.message
                                    });
                                $(‘#mTb‘).datagrid(‘reload‘);    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: ‘提示‘,
                                    msg: result.message
                                });
                            }
                        },‘json‘);
                    }
                });
            }
        }
        function doSearch(){
            $(‘#mTb‘).datagrid(‘load‘,{
                name: $(‘#username‘).val(),
                xueli: $(‘#userxueli‘).val()
            });
        }
  </script>

  <body>
      <table id="mTb"
          class="easyui-datagrid" width="100%"
          url="UserServlet?f=query"
          toolbar="#toolbar" pagination="true"
        rownumbers="true" fitColumns="true" singleSelect="true"
        pageSize="5"
        pageList="[3,5,8,10]">
        <thead>
            <tr>
                <th field="id" width="50" data-options="hidden: true">编号</th>
                <th field="name" width="50">姓名</th>
                <th field="pass" width="50">密码</th>
                <th field="sex" width="50">性别</th>
                <th field="age" width="50">年龄</th>
                <th field="xueli" width="50">学历</th>
                <th field="address" width="50">地址</th>
            </tr>
        </thead>
      </table>

      <div id="toolbar">
        <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新增</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">删除</a>
    </div>

    <div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle">用户信息</div>
        <form id="fm" method="post" novalidate>
            <div class="fitem">
                <label>姓名:</label>
                <input name="name" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>密码:</label>
                <input name="pass" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>性别:</label>
                <input name="sex" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>年龄:</label>
                <input name="age" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>学历:</label>
                <input name="xueli" class="easyui-validatebox" required="true">
            </div>
            <div class="fitem">
                <label>地址:</label>
                <input name="address" class="easyui-validatebox" required="true">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">提交</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$(‘#dlg‘).dialog(‘close‘)">取消</a>
    </div>

  </body>
</html>

3.Servlet代码:

package com.scme.servlet;

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

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

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.scme.dao.UserDao;
import com.scme.pojo.Userinfo;

public class UserServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public UserServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        UserDao dao = new UserDao();
        JsonObject jsonObject = new JsonObject();

        String flag = request.getParameter("f");
        if(flag.equals("query")) {
            List<Userinfo> mlist = dao.queryAll();
            out.write(new Gson().toJson(mlist));
        } else if(flag.equals("add")){
            Userinfo userinfo = new Userinfo();
            userinfo.setName(request.getParameter("name"));
            userinfo.setPass(request.getParameter("pass"));
            userinfo.setSex(request.getParameter("sex"));
            userinfo.setAge(request.getParameter("age"));
            userinfo.setXueli(request.getParameter("xueli"));
            userinfo.setAddress(request.getParameter("address"));

            System.out.println("name: " + userinfo.getName());

            if(dao.addUser(userinfo) == 1){
                jsonObject.addProperty("success", true);
                jsonObject.addProperty("message", "添加成功");
            } else {
                jsonObject.addProperty("success", false);
                jsonObject.addProperty("message", "添加失败");
            }
            out.write(jsonObject.toString());
        } else if(flag.equals("update")) {
            Userinfo userinfo = new Userinfo();
            int id = Integer.parseInt(request.getParameter("id"));
            userinfo.setName(request.getParameter("name"));
            userinfo.setPass(request.getParameter("pass"));
            userinfo.setSex(request.getParameter("sex"));
            userinfo.setAge(request.getParameter("age"));
            userinfo.setXueli(request.getParameter("xueli"));
            userinfo.setAddress(request.getParameter("address"));

            if(dao.updateUser(userinfo, id) == 1){
                jsonObject.addProperty("success", true);
                jsonObject.addProperty("message", "修改成功");
            } else {
                jsonObject.addProperty("success", false);
                jsonObject.addProperty("message", "修改失败");
            }
            out.write(jsonObject.toString());
        } else if(flag.equals("delete")) {
            int id = Integer.parseInt(request.getParameter("id"));
            if(dao.deleteUser(id) == 1){
                jsonObject.addProperty("success", true);
                jsonObject.addProperty("message", "删除成功");
            } else {
                jsonObject.addProperty("success", false);
                jsonObject.addProperty("message", "删除失败");
            }
            out.write(jsonObject.toString());
        }

        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

4.效果展示

时间: 2024-08-06 15:57:50

JqueryEasyUI实现CRUD增删改查操作的相关文章

SAP云平台以微服务的方式提供了Document的CRUD(增删改查)操作。该微服务基于标准的CMI

SAP云平台以微服务的方式提供了Document的CRUD(增删改查)操作.该微服务基于标准的CMIS协议(Content Management Interoperability Service). 同标准的CMIS相比,SAP云平台的Document Service增添了一些功能的支持: 通过一个Hello World应用来了解如何在Java程序里消费SAP云平台的Document Service. 通过这个链接下载例子程序. 点击该超链接下载Java Web Tomcat 8 SDK. 例子

创建支持CRUD(增删改查)操作的Web API

一:准备工作 你可以直接下载源码查看 Download the completed project.     下载完整的项目 CRUD是指“创建(C).读取(R).更新(U)和删除(D)”,它们是四个基本的数据库操作.许多HTTP服务也会通过REST或类REST的API模拟CRUD操作. 在本教程中,我们将建立一个十分简单的Web API来管理一列产品. 每个产品包含一个name(名称).price(价格)和category(分类)(如,“toys(玩具)”.“hardware(硬件)”等),还

Java+MyEclipse+Tomcat (六)详解Servlet和DAO数据库增删改查操作

此篇文章主要讲述DAO.Java Bean和Servlet实现操作数据库,把链接数据库.数据库操作.前端界面显示分模块化实现.其中包括数据的CRUD增删改查操作,并通过一个常用的JSP网站前端模板界面进行描述.参考前文: Java+MyEclipse+Tomcat (一)配置过程及jsp网站开发入门 Java+MyEclipse+Tomcat (二)配置Servlet及简单实现表单提交 Java+MyEclipse+Tomcat (三)配置MySQL及查询数据显示在JSP网页中 Java+MyE

(转)SQLite数据库增删改查操作

原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n).char(n).d

yii2-basic后台管理功能开发之二:创建CRUD增删改查

昨天实现了后台模板的嵌套,今天我们可以试着创建CRUD模型啦 刚开始的应该都是“套用”,不再打算细说,只把关键的地方指出来. CRUD即数据库增删改查操作.可以理解为yii2为我们做了一个组件,来实现基本的增删改查视图和操作. 1.创建数据库表 2.用gii生成model模型 3.用gii生成CRUD 需要注意的是CRUD生成的控制器的namespace,要和当前所在目录保持一致.否组路由会报错,找不到该页面等信息.

前端的CRUD增删改查的小例子

前端的CRUD增删改查的小例子 1.效果演示 2.相关代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } .box{ width: 300px; margin: 0 auto; } ul{

Scala对MongoDB的增删改查操作

=========================================== 原文链接: Scala对MongoDB的增删改查操作 转载请注明出处! =========================================== 依赖环境:jdk1.8.Scala 2.12.idea mongodb Driver:3.1.1.注意,mongo for scala的驱动涉及多个jar(如下图),依赖于mongo-java-driver.jar 这里使用的sbt管理依赖,直接在bu

作业员工信息表实现增删改查操作

有以下员工信息表 当然此表你在文件存储时可以这样表示 1 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件,实现增删改查操作 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 select  * from staff_table where dept = "IT" select  * from staff_table where enroll

【greenDAO3】 项目搭建与增删改查操作

最近需要开始一个新的项目了,考虑到既然是新项目了,那么一些常用的框架肯定也要用当下最火的!这次的新项目中涉及到了本地数据存储,很早前有个项目的本地数据库框架用的是ActiveAndroid,github找了下这个框架,发现已经两年多已经没有更新了.然后就想到了一直没有时间去涉及到的greenDAO,github一搜索,哦呦?star有5000+,并且依然保持着很高的更新频率,并且性能远远的高于activeAndroid(见下图),果断选用. 刚开始想偷偷懒,大致浏览了下greenDAO官网后就开