前端代码布局
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ include file='/common/header.jsp' %>
- 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">
- <title>Insert title heretitle>
- head>
- <body>
- <div class="layui-form-item">
- <div class="layui-inline">
- <label class="layui-form-label">验证手机label>
- <div class="layui-input-inline">
- <input type="tel" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
- div>
- div>
- <div class="layui-inline">
- <div class="layui-input-inline">
- <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交button>
- div>
- div>
- div>
-
-
-
-
-
-
-
- <table class="layui-hide" id="test" lay-filter="test">table>
-
- <script type="text/html" id="toolbarDemo">
- <div class="layui-btn-container">
- <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据button>
- <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目button>
- <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选button>
- div>
- script>
-
- <script type="text/html" id="barDemo">
- <a class="layui-btn layui-btn-xs" lay-event="edit">编辑a>
- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除a>
- script>
-
-
-
- <script>
- layui.use('table', function(){
- var table = layui.table;
-
- table.render({
- elem: '#test'
- ,url:''
- ,toolbar: '#toolbarDemo' //开启头部工具栏,并为其绑定左侧模板
- ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义,去除该参数即可
- title: '提示'
- ,layEvent: 'LAYTABLE_TIPS'
- ,icon: 'layui-icon-tips'
- }]
- ,title: '用户数据表'
- ,cols: [[
- {type: 'checkbox', fixed: 'left'}
- ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
- ,{field:'username', title:'用户名', width:120, edit: 'text'}
- ,{field:'email', title:'邮箱', width:150, edit: 'text', templet: function(res){
- return ''+ res.email +''
- }}
- ,{field:'sex', title:'性别', width:80, edit: 'text', sort: true}
- ,{field:'city', title:'城市', width:100}
- ,{field:'sign', title:'签名'}
- ,{field:'experience', title:'积分', width:80, sort: true}
- ,{field:'ip', title:'IP', width:120}
- ,{field:'logins', title:'登入次数', width:100, sort: true}
- ,{field:'joinTime', title:'加入时间', width:120}
- ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
- ]]
- ,page: true
- });
-
- //头工具栏事件
- table.on('toolbar(test)', function(obj){
- var checkStatus = table.checkStatus(obj.config.id);
- switch(obj.event){
- case 'getCheckData':
- var data = checkStatus.data;
- layer.alert(JSON.stringify(data));
- break;
- case 'getCheckLength':
- var data = checkStatus.data;
- layer.msg('选中了:'+ data.length + ' 个');
- break;
- case 'isAll':
- layer.msg(checkStatus.isAll ? '全选': '未全选');
- break;
-
- //自定义头工具栏右侧图标 - 提示
- case 'LAYTABLE_TIPS':
- layer.alert('这是工具栏右侧自定义的一个图标按钮');
- break;
- };
- });
-
- //监听行工具事件
- table.on('tool(test)', function(obj){
- var data = obj.data;
- //console.log(obj)
- if(obj.event === 'del'){
- layer.confirm('真的删除行么', function(index){
- obj.del();
- layer.close(index);
- });
- } else if(obj.event === 'edit'){
- layer.prompt({
- formType: 2
- ,value: data.email
- }, function(value, index){
- obj.update({
- email: value
- });
- layer.close(index);
- });
- }
- });
- });
- //Ajax获取
- $.post('url', {}, function(str){
- layer.open({
- type: 1,
- content: str //注意,如果str是object,那么需要字符拼接。
- });
- });
-
- script>
-
-
-
- body>
- html>
整体效果图如
后台代码如dao类
=============================查询=====================================================================
package com.zking.dao;
import java.util.List;
import java.util.Map;import com.zking.entity.User;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zking.util.StringUtils;public class UserDao extends BaseDao
{ public User login(User user )throws Exception {
String sql="select * from t_oa_user where loginName='"+user.getLoginName()+"' and pwd='"+user.getPwd()+"'";
Listusers= super.executeQuery(sql, User.class,null);
return users==null||users.size()==0 ?null:users.get(0);
}
public List
修改、删除增加等功能
//修改用户
public int edit(User user) throws Exception {
String sql="update t_oa_user set name=?,loginName=?,pwd=? where id=?";
return super.executeUpdate(sql, user, new String[] {"name","loginName","pwd","id"});
}
//删除用户public int del(User user) throws Exception {
String sql="delete t_oa_user where id=?";
return super.executeUpdate(sql, user, new String[] {"id"});
}
//增加用户
public int add(User user) throws Exception {
String sql="insert into t_oa_user (name,loginName,pwd) values(?,?,?)";
return super.executeUpdate(sql, user, new String[] {"name","loginName","pwd"});
}
UserAction类
- package com.zking.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.zking.dao.UserDao;
- import com.zking.entity.User;
- 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 UserAction extends ActionSupport implements ModelDriver<User>{
- private User user=new User();
- private UserDao userdao=new UserDao();
- //写一个方法处理前台的请求
- //作用传参数
- @Override
- public String execute(HttpServletRequest req, HttpServletResponse resp) {
- Map<String , Object> map=new HashMap<String, Object>();
- if("张三".equals(user.getLoginName())&&"123456".equals(user.getPwd())) {
- // 登录成功
- map.put("code", 200);
- map.put("msg", "成功");
- }
- else {
- // 登录失败!
- map.put("code", 0);
- map.put("msg", "账户名或者密码有误!");
- }
- try {
- ResponseUtil.writeJson(resp, map);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- //
-
-
-
- // //简化代码
- // @Override
- // public String execute(HttpServletRequest req, HttpServletResponse resp) {
- // Map
map = new HashMap(); - // if ("张三".equals(u.getUsername()) && "123456".equals(u.getPassword())) {
- // try {
- // ResponseUtil.writeJson(resp, new R().data("code", 200).data("msg", "成功"));
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // } else {
- // try {
- // ResponseUtil.writeJson(resp, new R().data("code", 0).data("msg", "失败"));
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- //
- // }
- // try {
- // ResponseUtil.writeJson(resp, map);
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // return null;
- // }
- //
- public String login(HttpServletRequest req, HttpServletResponse resp) {
-
- try {
-
- User u=userdao.login(user);
- if(u!=null) {
- ResponseUtil.writeJson(resp, R.ok(200, "成功"));
- }
- else {
- ResponseUtil.writeJson(resp, R.ok(0, "用户名或者密码错误"));
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- try {
- ResponseUtil.writeJson(resp, R.ok(0, "用户名或者密码错误"));
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
-
- public String list(HttpServletRequest req, HttpServletResponse resp) {
-
- try {
- PageBean pagebean=new PageBean();
- pagebean.setRequest(req);
- List<Map<String, Object>> users=userdao.list(user, pagebean);
- ResponseUtil.writeJson(resp, R.ok(0,"用户数据查询成功",pagebean.getTotal()));
-
- } catch (Exception e) {
- e.printStackTrace();
- try {
- ResponseUtil.writeJson(resp, R.ok(0, "用户名或者密码错误"));
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
-
- public String add(HttpServletRequest req, HttpServletResponse resp) {
-
- try {
-
- int rs=userdao.add(user);
- if(rs>0) {
- ResponseUtil.writeJson(resp, R.ok(200, "用户主增加成功"));
- }
- else {
- ResponseUtil.writeJson(resp, R.ok(0, "用户数据查询失败"));
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- try {
- ResponseUtil.writeJson(resp, R.ok(0, "用户数据查询失败"));
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
-
- public String del(HttpServletRequest req, HttpServletResponse resp) {
-
- try {
-
- int rs=userdao.del(user);
- if(rs>0) {
- ResponseUtil.writeJson(resp, R.ok(200, "用户主删除成功"));
- }
- else {
- ResponseUtil.writeJson(resp, R.ok(0, "用户数据删除失败"));
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- try {
- ResponseUtil.writeJson(resp, R.ok(0, "用户数据删除失败"));
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
-
-
-
- public String edit(HttpServletRequest req, HttpServletResponse resp) {
-
- try {
-
- int rs=userdao.edit(user);
- if(rs>0) {
- ResponseUtil.writeJson(resp, R.ok(200, "用户主修改成功"));
- }
- else {
- ResponseUtil.writeJson(resp, R.ok(0, "用户数据修改失败"));
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- try {
- ResponseUtil.writeJson(resp, R.ok(0, "用户数据修改失败"));
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return null;
- }
-
-
-
-
-
- @Override
- public User getModel() {
- return user;
- }
-
- }
UserDaoTest测试类
package com.zking.dao;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Map;import org.junit.After;
import org.junit.Before;
import org.junit.Test;import com.zking.entity.User;
import com.zking.util.PageBean;/**
* @author lucy
* junit测试类
*
*/
public class UserDaoTest {
private UserDao userdao=new UserDao();
@Before
public void setUp() throws Exception {
}@After
public void tearDown() throws Exception {
}
@Test
public void testLogin() {
fail("Not yet implemented");
}@Test
public void testList() {//查询
PageBean pagebean =new PageBean();
User user=new User();
try {
List@Test
public void testEdit() {//修改
User user =new User();
user.setName("李哈哈");
user.setLoginName("lhh");
user.setPwd("123");
user.setId(17L);
try {
userdao.edit(user);
} catch (Exception e) {
e.printStackTrace();
}
}@Test
public void testDel() {//删除
User user =new User();
user.setId(17L);
try {
userdao.del(user);
} catch (Exception e) {
e.printStackTrace();
}
}@Test
public void testAdd() {//增加
User user =new User();
user.setName("李哈哈");
user.setLoginName("lhh");
user.setPwd("123");
try {
userdao.add(user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
list集合
userManager.js
- /3.对话框
- function open(title){
- layer.open({
- type: 2, //layer提供了5种层类型。可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层)
- title:title,
- area: ['660px', '340px'], //宽高
- skin: 'layui-layer-rim', //样式类名
- content: $("#ctx").val()+'/jsp/system/userEdit.jsp', //书本编辑页面
- btn:['保存','关闭','批量新增'],
- yes: function(index, layero){
- //调用子页面中提供的getData方法,快速获取子页面的form表单数据
- let data= $(layero).find("iframe")[0].contentWindow.getData();
- console.log(data);
- //判断title标题
- let methodName="add";
- if(title=="编辑")
- methodName="edit";
- $.post($("#ctx").val()+'/user.action?methodName='+methodName,
- data,function(rs){
- if(rs.success){
- //关闭对话框
- layer.closeAll();
- //调用查询方法刷新数据
- query();
- }else{
- layer.msg(rs.msg,function(){});
- }
- },'json');
- },
- btn2: function(index, layero){
- layer.closeAll();
- },
- btn3: function(index, layero){
- layer.msg("批量新增");
- return false;//不关闭弹出层
- }
- });
- }
header.jsp头部导入
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
前端代码userManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file='/common/header.jsp' %>
效果图
useredit.js
let layer,form,$;
layui.use(['layer','form','jquery'],function(){
layer=layui.layer,form=layui.form,$=layui.jquery;
initData();
});function initData(){
console.log(parent.row);
if(null!=parent.row){
//因为layui.each内部的逻辑问题导致的所以要先深拷贝一份然后再去val
//parent.row:表格行对象
form.val('user',$.extend({}, parent.row||{}));
//$('#name').attr('readonly','readonly');
}
}function getData(){
return form.val('user');
}
useredit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/common/header.jsp"%>
用户新增
userManager.js
//在页面中的
中必须配置lay-filter="tb_goods"属性才能触发属性!!!
table.on('tool(tb)', function (obj) {
row = obj.data;
if (obj.event == "edit") {
open("编辑");
}else if(obj.event == "del"){
layer.confirm('确认删除吗?', {icon: 3, title:'提示'}, function(index){
$.post($("#ctx").val()+'/user.action',{
'methodName':'del',
'id':row.id
},function(rs){
if(rs.success){
//调用查询方法刷新数据
query();
}else{
layer.msg(rs.msg,function(){});
}
},'json');
layer.close(index);
});
}else{}
});
}
- 相关阅读:
[七夕节]——一款基于canvas绘制五颜六色线条合成一个爱心发光动画特效
第一篇:参考资料地址
【Pandas包】-文章汇总
Docker Compose和Consul
计算机毕业设计(附源码)python游戏盒子系统
电容笔有什么用?电容笔十大品牌排行
Qt编写物联网管理平台36-通信协议
推荐一个高效测试用例工具:XMind2TestCase..
SystemVerilog-决策语句-case语句
ArrayList和CopyOnWriteArrayList
- 原文地址:https://blog.csdn.net/qq_66924116/article/details/125891623