后台管理完成50%

This commit is contained in:
landaiqing 2023-06-10 00:07:58 +08:00
parent 489f5dbd12
commit aa044b3c03
11 changed files with 903 additions and 71 deletions

View File

@ -3,23 +3,22 @@ package com.landaiqing.dao;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import java.util.ArrayList;
public class AdminUserDao {
private AdminUserEntity adminUserEntity=new AdminUserEntity();
private AdminUserEntity adminUserEntity = new AdminUserEntity();
/**
* 管理员登录
* */
*/
public AdminUserEntity login(String userName, String userPwd) {
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
String loginSql = "select * from admin where adminUserName=? and adminPassword=?;";
String loginSql = "select * from admin where adminUserName=? and adminPassword=? and is_valid='1';";
preparedStatement = connection.prepareStatement(loginSql);
preparedStatement.setString(1, userName);
preparedStatement.setString(2, userPwd);
@ -40,9 +39,10 @@ public class AdminUserDao {
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
}
}
/**
* 查询所有管理员
* */
*/
public ArrayList<AdminUserEntity> selectAllAdmin() {
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
@ -52,12 +52,13 @@ public class AdminUserDao {
String loginSql = "select * from admin;";
preparedStatement = connection.prepareStatement(loginSql);
resultSet = preparedStatement.executeQuery();
ArrayList<AdminUserEntity> adminUserDaos=new ArrayList<>();
ArrayList<AdminUserEntity> adminUserDaos = new ArrayList<>();
while (resultSet.next()) {
Integer id = resultSet.getInt(1);
String dbUserName = resultSet.getString(2);
String dbUserPwd = resultSet.getString(3);
AdminUserEntity adminUserEntity = new AdminUserEntity(id,dbUserName, dbUserPwd);
Integer isValid = resultSet.getInt(4);
AdminUserEntity adminUserEntity = new AdminUserEntity(id, dbUserName, dbUserPwd, isValid);
adminUserDaos.add(adminUserEntity);
}
return adminUserDaos;
@ -68,4 +69,164 @@ public class AdminUserDao {
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
}
}
/**
* 打开管理员状态
*/
public int OpenAdmin(Integer id) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("UPDATE `admin` SET `is_valid` = ? WHERE `adminId` = ?;");
preparedStatement.setInt(1, 1);
preparedStatement.setInt(2, id);
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
/**
* 关闭管理员状态
*/
public int CloseAdmin(Integer id) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("UPDATE `admin` SET `is_valid` = ? WHERE `adminId` = ?;");
preparedStatement.setInt(1, 0);
preparedStatement.setInt(2, id);
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
/**
* 通过ID查询管理员
*/
public AdminUserEntity getAdminByID(Integer id) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
preparedStatement = connection.prepareStatement("select * from admin where adminId=?;");
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) {
return null;
}
Integer adminId = resultSet.getInt("adminId");
String adminUserName = resultSet.getString("adminUserName");
String adminPassword = resultSet.getString("adminPassword");
Integer isValid = resultSet.getInt("is_valid");
AdminUserEntity adminUserEntity1 = new AdminUserEntity(adminId, adminUserName, adminPassword, isValid);
return adminUserEntity1;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
}
}
/**
* 修改管理员信息
*/
public int updateAdmin(AdminUserEntity userEntity) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("UPDATE `admin` SET `adminUserName` = ?, `adminPassword` = ?,`is_valid`=? WHERE `adminId` = ?;");
preparedStatement.setString(1, userEntity.getAdminUserName());
preparedStatement.setString(2, userEntity.getAdminPassword());
preparedStatement.setInt(3, userEntity.getIsValid());
preparedStatement.setInt(4, userEntity.getAdminId());
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
/**
* 添加管理员
* */
public int insertAdmin(AdminUserEntity adminUserEntity){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("INSERT INTO `admin` (`adminId`, `adminUserName`, `adminPassword`, `is_valid`) VALUES (null, ?, ?, ?);");
preparedStatement.setString(1,adminUserEntity.getAdminUserName());
preparedStatement.setString(2,adminUserEntity.getAdminPassword());
preparedStatement.setInt(3,adminUserEntity.getIsValid());
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
/**
* 删除管理源
* */
public int deleteAdmin(Integer id){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("DELETE from `admin` WHERE `adminId` = ?;");
preparedStatement.setInt(1,id);
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
}

View File

@ -15,6 +15,16 @@ public class AdminUserEntity {
private Integer adminId;
private String adminUserName;
private String adminPassword;
private Integer isValid;
public AdminUserEntity(String adminUserName, String adminPassword) {
this.adminUserName = adminUserName;
this.adminPassword = adminPassword;
}
public AdminUserEntity(){
}
public Integer getAdminId() {
return adminId;
@ -40,18 +50,18 @@ public class AdminUserEntity {
this.adminPassword = adminPassword;
}
public AdminUserEntity(Integer adminId, String adminUserName, String adminPassword) {
public Integer getIsValid() {
return isValid;
}
public void setIsValid(Integer isValid) {
this.isValid = isValid;
}
public AdminUserEntity(Integer adminId, String adminUserName, String adminPassword, Integer isValid) {
this.adminId = adminId;
this.adminUserName = adminUserName;
this.adminPassword = adminPassword;
}
public AdminUserEntity(String adminUserName, String adminPassword) {
this.adminUserName = adminUserName;
this.adminPassword = adminPassword;
}
public AdminUserEntity(){
this.isValid = isValid;
}
}

View File

@ -1,48 +1,48 @@
package com.landaiqing.filter;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
* 过滤器
*/
@WebFilter("/System/*")// 过滤器所有的请求
public class UserSessionFilter implements Filter {
private String[] excludeUrls = new String[]{"/login", "/publish","/reply","/VerifycodeServlet"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// 从session获取到用户的会话信息 判断用户是否登录过
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
String contextPath = httpServletRequest.getContextPath();
// 定义一个数组 哪些 请求是需要排除的
for (int i = 0; i < excludeUrls.length; i++) {
String excludeUrl = contextPath + excludeUrls[i];
String requestURI = httpServletRequest.getRequestURI();
if (excludeUrl.equals(requestURI)) {
// 放行请求
filterChain.doFilter(httpServletRequest, httpServletResponse);
return;
}
}
// 排除请求
HttpSession session = httpServletRequest.getSession();
Object user = session.getAttribute("user");
if (user == null) {
// 当前用户没有登录或者登录会话失效
// 重定向到登录页面
httpServletResponse.sendRedirect(contextPath+"/");
return;
}
// 用户已经登录了 正常放行请求
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
//package com.landaiqing.filter;
//
//
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import jakarta.servlet.http.HttpSession;
//
//import java.io.IOException;
//
///**
// * 过滤器
// */
//@WebFilter("/System/*")// 过滤器所有的请求
//public class UserSessionFilter implements Filter {
// private String[] excludeUrls = new String[]{"/login", "/publish","/reply","/VerifycodeServlet","/System/showAdmin"};
//
// @Override
// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// // 从session获取到用户的会话信息 判断用户是否登录过
// HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
// String contextPath = httpServletRequest.getContextPath();
// // 定义一个数组 哪些 请求是需要排除的
// for (int i = 0; i < excludeUrls.length; i++) {
// String excludeUrl = contextPath + excludeUrls[i];
// String requestURI = httpServletRequest.getRequestURI();
// if (excludeUrl.equals(requestURI)) {
// // 放行请求
// filterChain.doFilter(httpServletRequest, httpServletResponse);
// return;
// }
// }
// // 排除请求
// HttpSession session = httpServletRequest.getSession();
// Object user = session.getAttribute("user");
// if (user == null) {
// // 当前用户没有登录或者登录会话失效
// // 重定向到登录页面
// httpServletResponse.sendRedirect(contextPath+"/");
// return;
// }
// // 用户已经登录了 正常放行请求
// filterChain.doFilter(httpServletRequest, httpServletResponse);
// }
//}
//

View File

@ -21,4 +21,48 @@ public class AdminUserService {
public ArrayList<AdminUserEntity> selectAllAdmin() {
return adminUserDao.selectAllAdmin();
}
/**
* 修改管理员的状态
* */
public int OpenAdmin(Integer id){
return adminUserDao.OpenAdmin(id);
}
/**
* 关闭管理员的状态
* */
public int CloseAdmin(Integer id){
return adminUserDao.CloseAdmin(id);
}
/**
* 通过ID查询管理员
*/
public AdminUserEntity getAdminByID(Integer id) {
return adminUserDao.getAdminByID(id);
}
/**
* 修改管理员信息
*/
public int updateAdmin(AdminUserEntity userEntity) {
return adminUserDao.updateAdmin(userEntity);
}
/**
* 添加管理员
* */
public int insertAdmin(AdminUserEntity adminUserEntity){
return adminUserDao.insertAdmin(adminUserEntity);
}
/**
* 删除管理源
* */
public int deleteAdmin(Integer id){
return adminUserDao.deleteAdmin(id);
}
}

View File

@ -0,0 +1,61 @@
package com.landaiqing.servlet.system;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.landaiqing.service.UserService;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/addAdmin")
public class AddAdmin extends HttpServlet {
private AdminUserService adminUserService=new AdminUserService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String adminUseName = req.getParameter("adminUserName");
if (StringUtils.isNullOrEmpty(adminUseName)){
req.setAttribute("errorMsg","adminUseName 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String adminPassword = req.getParameter("adminPassword");
if (StringUtils.isNullOrEmpty(adminPassword)){
req.setAttribute("errorMsg","adminPassword 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String isValid = req.getParameter("isValid");
if (StringUtils.isNullOrEmpty(isValid)){
req.setAttribute("errorMsg","isValid 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
AdminUserEntity adminUserEntity=new AdminUserEntity();
adminUserEntity.setAdminUserName(adminUseName);
adminUserEntity.setAdminPassword(adminPassword);
adminUserEntity.setIsValid(Integer.valueOf(isValid));
int result = adminUserService.insertAdmin(adminUserEntity);
if (result<=0){
req.setAttribute("errorMsg","插入失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
resp.sendRedirect("./System/index.jsp");
} catch (Exception e) {
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
throw new RuntimeException(e);
}
}
}

View File

@ -1,5 +1,6 @@
package com.landaiqing.servlet.system;
import com.alibaba.fastjson.JSONObject;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import jakarta.servlet.ServletException;
@ -9,10 +10,11 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/allAdmin")
@WebServlet("/adminList")
public class AllAdmin extends HttpServlet {
private AdminUserService adminUserService=new AdminUserService();
@Override
@ -23,8 +25,13 @@ public class AllAdmin extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<AdminUserEntity> adminUserEntityList=adminUserService.selectAllAdmin();
req.setAttribute("allAdmin",adminUserEntityList);
req.getRequestDispatcher("./System/index.jsp").forward(req,resp);
PrintWriter writer=resp.getWriter();
String jsonString = JSONObject.toJSONString(adminUserEntityList);
System.out.println(jsonString);
writer.println(jsonString);
writer.close();
// req.setAttribute("allAdmin",adminUserEntityList);
// req.getRequestDispatcher("./System/index.jsp").forward(req,resp);
}
}

View File

@ -0,0 +1,44 @@
package com.landaiqing.servlet.system;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
@WebServlet("/closeAdmin")
public class CloseAdmin extends HttpServlet {
private AdminUserService adminUserService=new AdminUserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String idStr = req.getParameter("id");
if (StringUtils.isNullOrEmpty(idStr)){
req.setAttribute("errorMsg","id的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
int result = adminUserService.CloseAdmin(Integer.valueOf(idStr));
if (result<=0){
req.setAttribute("errorMsg","修改失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
PrintWriter writer=resp.getWriter();
writer.println("修改成功!");
writer.close();
resp.sendRedirect("");
} catch (Exception e) {
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,44 @@
package com.landaiqing.servlet.system;
import com.landaiqing.service.AdminUserService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/deleteAdmin")
public class DeleteAdmin extends HttpServlet {
private AdminUserService adminUserService=new AdminUserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id");
if (idStr==null || idStr==""){
req.setAttribute("errorMsg","ID的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
try {
Integer id = Integer.parseInt(idStr);
int result = adminUserService.deleteAdmin(id);
if (result > 0) {
// req.getRequestDispatcher("showFlight.jsp").forward(req,resp);
resp.sendRedirect("./System/index.jsp");
}else {
req.setAttribute("errorMsg","删除失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
}
} catch (NumberFormatException e) {
req.setAttribute("errorMsg","类型转换异常,id 不能转换成Int类型!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
e.printStackTrace();
}catch (Exception e){
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
}
}
}

View File

@ -0,0 +1,92 @@
package com.landaiqing.servlet.system;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/updateAdmin")
public class GetAdminByID extends HttpServlet {
private AdminUserService adminUserService=new AdminUserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String idStr = req.getParameter("id");
if (idStr==null || idStr==""){
req.setAttribute("errorMsg","ID的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
try {
Integer id = Integer.parseInt(idStr);
AdminUserEntity adminUserEntity = adminUserService.getAdminByID(id);
if (adminUserEntity==null){
req.setAttribute("errorMsg","id不存在!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
req.setAttribute("adminByID",adminUserEntity);
req.getRequestDispatcher("./System/adminManage.jsp").forward(req,resp);
}catch (Exception e){
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String idStr = req.getParameter("id");
if (StringUtils.isNullOrEmpty(idStr)){
req.setAttribute("errorMsg","id的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
Integer id = Integer.parseInt(idStr);
String adminUserName = req.getParameter("adminUserName");
if (StringUtils.isNullOrEmpty(adminUserName)){
req.setAttribute("errorMsg","adminUserName 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String adminPassword = req.getParameter("adminPassword");
if (StringUtils.isNullOrEmpty(adminPassword)){
req.setAttribute("errorMsg","adminPassword 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String isValid = req.getParameter("isValid");
if (StringUtils.isNullOrEmpty(idStr)){
req.setAttribute("errorMsg","isValid 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
AdminUserEntity adminUserEntity=new AdminUserEntity();
adminUserEntity.setAdminId(id);
adminUserEntity.setAdminUserName(adminUserName);
adminUserEntity.setAdminPassword(adminPassword);
adminUserEntity.setIsValid(Integer.valueOf(isValid));
int result = adminUserService.updateAdmin(adminUserEntity);
if (result<=0){
req.setAttribute("errorMsg","修改失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
resp.sendRedirect("./System/adminManage.jsp");
} catch (Exception e) {
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,44 @@
package com.landaiqing.servlet.system;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
@WebServlet("/openAdmin")
public class OpenAdmin extends HttpServlet {
private AdminUserService adminUserService=new AdminUserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String idStr = req.getParameter("id");
if (StringUtils.isNullOrEmpty(idStr)){
req.setAttribute("errorMsg","id的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
int result = adminUserService.OpenAdmin(Integer.valueOf(idStr));
if (result<=0){
req.setAttribute("errorMsg","修改失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
PrintWriter writer=resp.getWriter();
writer.println("修改成功!");
writer.close();
resp.sendRedirect("");
} catch (Exception e) {
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
throw new RuntimeException(e);
}
}
}

View File

@ -9,8 +9,333 @@
<html>
<head>
<title>Title</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<!-- 引入 layui.css -->
<link href="//unpkg.com/layui@2.8.0/dist/css/layui.css" rel="stylesheet">
<!-- 引入 layui.js -->
<script src="//unpkg.com/layui@2.8.0/dist/layui.js"></script>
</head>
<body>
管理员管理
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="addAdmin">添加管理员</button>
</div>
</script>
<table class="layui-hide" id="ID-table-demo-data"></table>
<script type="text/html" id="ID-table-demo-templet-switch">
<!-- 这里的 checked 的状态值判断仅作为演示 -->
<input type="checkbox" id="checkbox" name="status" value="{{= d.adminId }}" title="开启|关闭" lay-skin="switch" lay-filter="status" {{= d.isValid == '1' ? "checked" : "" }}>
</script>
<script type="text/html" id="barDemo">
<div class="layui-clear-space">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-xs" lay-event="more">
更多
<i class="layui-icon layui-icon-down"></i>
</a>
</div>
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
function getDate(){
axios({
// 请求方式
method: 'get',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/adminList',
// URL 中的查询参数
params: {
}
}).then(function (result) {
layui.use('table', function(){
var table = layui.table;
var form = layui.form;
var dropdown = layui.dropdown;
// 已知数据渲染
var inst = table.render({
elem: '#ID-table-demo-data'
,toolbar: '#toolbarDemo'
,cols: [[ //标题栏
{field: 'adminId', title: 'ID', width: 80, sort: true}
,{field: 'adminUserName', title: '管理员', width: 180}
,{field: 'adminPassword', title: '密码', width: 200}
,{field: 'isValid', title: '是否有效', width: 150,templet: function(d){
if(d.isValid === 1){
return '<span style="color: green" title="有效">●</span>';
} else {
return '<span style="color: red" title="无效">●</span>';
}
}},
{title: '状态', width:100, templet: '#ID-table-demo-templet-switch'},
{fixed: 'right', title:'操作', width: 134, minWidth: 125, toolbar: '#barDemo'}
]],done:function (){
form.on('switch(status)', function (obj) {
var id = obj.value;
if(this.checked){
OpenAdmin(id);
}else{
CloseAdmin(id);
}
});
table.on('toolbar(ID-table-demo-data)', function(obj){
var id = obj.config.id;
var checkStatus = table.checkStatus(id);
var othis = lay(this);
switch(obj.event){
case 'addAdmin':
layer.open({
title: '添加管理员',
type: 1,
area: ['50%','80%'],
content: `<div id="warp">
<div style="display: flex;flex-direction: column;align-content: center;align-items: center;justify-content: center">
<div class="layui-form-item">
<label class="layui-form-label">管理员</label>
<div class="layui-input-block">
<input type="text" name="admin" id="adminUserName" autocomplete="off" class="layui-input" >
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" id="adminPassword" lay-verify="required" class="layui-input" >
</div>
</div>
<div class="layui-form-item" pane>
<label class="layui-form-label">是否有效</label>
<div class="layui-input-block">
<input type="text" id="isValid" name="open" lay-filter="required" class="layui-input" >
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" onclick="addAdmin()">确认</button>
<!-- <button type="close" class="layui-btn layui-btn-primary" onclick="close()">关闭</button>-->
</div>
</div>
</div>`,
});
break;
// case 'getData':
// var getData = table.getData(id);
// console.log(getData);
// layer.alert(layui.util.escape(JSON.stringify(getData)));
// break;
// case 'LAYTABLE_TIPS':
// layer.alert('自定义工具栏图标按钮');
// break;
};
});
// 触发单元格工具事件
table.on('tool(ID-table-demo-data)', function(obj){ // 双击 toolDouble
var data = obj.data; // 获得当前行数据
// console.log(obj)
if(obj.event === 'edit'){
layer.open({
title: '编辑 - id:'+ data.adminId,
type: 1,
area: ['50%','80%'],
content: `<div id="warp">
<div style="display: flex;flex-direction: column;align-content: center;align-items: center;justify-content: center">
<div class="layui-form-item">
<label class="layui-form-label">ID</label>
<div class="layui-input-block">
<input type="text" name="id" id="ID" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">管理员</label>
<div class="layui-input-block">
<input type="text" name="admin" id="adminUserName" autocomplete="off" class="layui-input" >
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" id="adminPassword" lay-verify="required" class="layui-input" >
</div>
</div>
<div class="layui-form-item" pane>
<label class="layui-form-label">是否有效</label>
<div class="layui-input-block">
<input type="text" id="isValid" name="open" lay-filter="required" class="layui-input" >
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" onclick="upAdateAdmin()">确认</button>
<!-- <button type="close" class="layui-btn layui-btn-primary" onclick="close()">关闭</button>-->
</div>
</div>
</div>`,
});
} else if(obj.event === 'more'){
// 更多 - 下拉菜单
dropdown.render({
elem: this, // 触发事件的 DOM 对象
show: true, // 外部事件触发即显示
data: [{
title: '查看',
id: 'detail'
},{
title: '删除',
id: 'del'
}],
click: function(menudata){
if(menudata.id === 'detail'){
layer.msg('ID:'+ data.adminId
+'\n管理员:'+data.adminUserName
+"\n密 码:"+data.adminPassword
+"\n是否有效:"+data.isValid);
} else if(menudata.id === 'del'){
layer.confirm('真的删除行 [id: '+ data.adminId +'] 么', function(){
obj.del(); // 删除对应行tr的DOM结构
// layer.close(index);
deleteAdmin(data.adminId);
// 向服务端发送删除指令
});
}
},
align: 'right', // 右对齐弹出
style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
})
}
});
}
,data: result.data
//,skin: 'line' // 表格风格
//,even: true
,page: true // 是否显示分页
,limits: [5, 10, 15]
,limit: 5 // 每页默认显示的数量
});
});
})
}
getDate();
function OpenAdmin(id){
axios({
// 请求方式
method: 'get',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/openAdmin',
// URL 中的查询参数
params: {
id:id
}
}).then(function (result) {
layer.msg('开启成功'), {
offset: '6px'
}
})
}
function CloseAdmin(id){
axios({
// 请求方式
method: 'get',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/closeAdmin',
// URL 中的查询参数
params: {
id:id
}
}).then(function (result) {
layer.msg('关闭成功'), {
offset: '6px'
}
})
}
function upAdateAdmin(){
var id=document.getElementById('ID').value;
var adminUserName=document.getElementById('adminUserName').value;
var adminPassword=document.getElementById('adminPassword').value;
var isValid=document.getElementById('isValid').value;
axios({
// 请求方式
method: 'post',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/updateAdmin',
// URL 中的查询参数
params: {
id:id,
adminUserName:adminUserName,
adminPassword:adminPassword,
isValid:isValid
}
}).then(function (result) {
layer.msg('修改成功!'), {
offset: '6px'
}
})
}
function deleteAdmin(id){
axios({
// 请求方式
method: 'get',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/deleteAdmin',
// URL 中的查询参数
params: {
id:id,
}
}).then(function (result) {
layer.msg('删除成功!'), {
offset: '6px'
}
})
}
function addAdmin()
{
var adminUserName=document.getElementById('adminUserName').value;
var adminPassword=document.getElementById('adminPassword').value;
var isValid=document.getElementById('isValid').value;
axios({
// 请求方式
method: 'post',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/addAdmin',
// URL 中的查询参数
params: {
adminUserName:adminUserName,
adminPassword:adminPassword,
isValid:isValid
}
}).then(function (result) {
layer.msg('添加成功!'), {
offset: '6px'
}
})
}
</script>
</body>
</html>