IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> ?基于bootstap-Jquery-JSP-Servlet-mysql?博客项目——后台资源管理demo2 -> 正文阅读

[JavaScript知识库]?基于bootstap-Jquery-JSP-Servlet-mysql?博客项目——后台资源管理demo2

上一篇文章中实现了后台资源管理中的用户资源管理,这一篇介绍资源类别管理的实现

资源类别管理

获取所有资源类别信息并分页

在controller中的resource包下创建DoCategorySelect.java,代码如下

@WebServlet("/manage/admin_do_category_select")
public class DoCategorySelect extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 当前页
		int currentPage = 1;
		// 每页显示条数
		int count = 5;
		// array[0]=资源类别总数,array[1]=总页数,后面会被覆盖
		int array[] = { 0, 0 };
		// 获取用户指定的页面
		String cp = request.getParameter("currentPage");
		System.out.println("cp = " + cp);
		// 接收用户搜索的关键字
		String keyword = request.getParameter("keywords");
		/* 用户如果有指定当前页,则把字符串转化为int型 */
		if (cp != null) {
			currentPage = Integer.parseInt(cp);
		}
		// 创建资源service层接口的实现类
		ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
		/*
		 * 调用业务逻辑方法获取资源类别记录总数并算出总页数 即(array[0]=资源类别总数,array[1]=总页数(资源类别总数/每页显示条数)
		 */
		try {
			array = resourceServiceImpl.getRsCategoryPageTotal(count, keyword);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 调用业务逻辑方法获取所有资源类别信息
		ArrayList<Category> listCategory = null;
		try {
			listCategory = resourceServiceImpl.getAllRsCategory(currentPage, count, keyword);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 放到请求对象域里
		request.setAttribute("categoryList", listCategory);
		request.setAttribute("totalStudent", array[0]);
		request.setAttribute("totalPage", array[1]);
		request.setAttribute("currentPage", currentPage);
		/* 若用户是通过搜索的,把关键词放在请求域中 */
		if (keyword != null) {
			request.setAttribute("searchParams", "&keywords=" + keyword);
		}
		request.getRequestDispatcher("/WEB-INF/manage/admin_category.jsp").forward(request, response);
	}
}

在service层的ResourceService接口中添加如下业务逻辑抽象方法

	// 获取所有资源类别记录总数及页数
	public int[] getRsCategoryPageTotal(int count, String keyword) throws SQLException;
	
	// 获取所有资源的的类别信息
	public ArrayList<Category> getAllRsCategory(int currentPage, int count, String keyword) throws SQLException;

在service层的实现类ResourceServiceImpl中重写接口方法,如下

	@Override
	public int[] getRsCategoryPageTotal(int count, String keyword) throws SQLException {
		// 检索所有资源类别总数并算出总页数
		int[] array = resourceDao.selectRsCategoryTotal(count, keyword);
		return array;
	}
	
	@Override
	public ArrayList<Category> getAllRsCategory(int currentPage, int count, String keyword) throws SQLException {
		// 检索所有资源的类别信息
		ArrayList<Category> listCategory = resourceDao.selectAllRsCategory(currentPage, count, keyword);
		return listCategory;
	}

在dao层的ResourceDao接口中添加添加如下抽象方法,执行底层操作,如下

	// 检索所有资源类别总数并算出总页数
	public int[] selectRsCategoryTotal(int count, String keyword) throws SQLException;
	
	// 检索所有资源的资源类别
	public ArrayList<Category> selectAllRsCategory(int currentPage, int count, String keyword) throws SQLException;

在dao层的实现类ResourceDaoImpl中重写接口方法,如下

	@Override
	public int[] selectRsCategoryTotal(int count, String keyword) throws SQLException {
		String sql = "";
		// 0 资源类别总记录数 1 页数
		int array[] = { 0, 1 };
		if (keyword != null) {
			sql = "select count(*) from category where  category_name like ?";
			resultSet = JDBCUtil.executeQuery(sql, "%" + keyword + "%");
		} else {
			sql = "select count(*) from category";
			resultSet = JDBCUtil.executeQuery(sql);
		}
		while (resultSet.next()) {
			array[0] = resultSet.getInt(1);
			if (array[0] % count == 0) {
				array[1] = array[0] / count;
			} else {
				array[1] = array[0] / count + 1;
			}
		}
		return array;
	}
	
	@Override
	public ArrayList<Category> selectAllRsCategory(int currentPage, int count, String keyword) throws SQLException {
		ArrayList<Category> listCategory = new ArrayList<Category>();
		String sql = "";
		if (keyword != null) {
			sql = "select * from category where  category_name like ?  limit ?, ?";
			resultSet = JDBCUtil.executeQuery(sql, "%" + keyword + "%", (currentPage - 1) * count, count);
		} else {
			sql = "select * from category  limit ?, ?";
			resultSet = JDBCUtil.executeQuery(sql, (currentPage - 1) * count, count);
		}
		while (resultSet.next()) {
			Category category = new Category(Integer.parseInt(resultSet.getString("Category_id")),
					resultSet.getString("Category_name"), resultSet.getString("Category_desc"));
			listCategory.add(category);
		}
		return listCategory;
	}

后台资源类别管理页面

在manage文件夹中创建admin_category.jsp,代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="admin_menu.jsp"%>
<div class="main-wrap">
	<div class="crumb-wrap">
		<div class="crumb-list">
			<i class="icon-font"></i><a
				href="${pageContext.request.contextPath}/manage/admin_index">首页</a><span
				class="crumb-step">&gt;</span><span class="crumb-name">类别管理</span>
		</div>
	</div>
	<div class="search-wrap">
		<div class="search-content">
			<form action="admin_do_category_select" method="get">
				<table class="search-tab">
					<tr>
						<th width="70">关键字:</th>
						<td><input class="common-text" placeholder="输入资源类别名称"
							name="keywords" id="" type="text"></td>
						<td><input class="btn btn-primary btn2" name="sub" value="查询"
							type="submit"></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<div id="register" class="result-wrap">
		<form action="admin_do_category_delete" id="delectForm" method="post">
		<input type="hidden" name="currentPage" value="${currentPage }">
			<div class="result-title">
				<div class="result-list">
					<a
						href="${pageContext.request.contextPath}/manage/admin_to_category_add">
						<i class="icon-font"></i>添加类别
					</a> <a id="batchDel"
						href="javascript:deleteMore('你确定删除这些资源类别吗?', 'delectForm')"> <i
						class="icon-font"></i>批量删除
					</a>
				</div>
			</div>
			<div class="result-content" id="dg">
				<table class="result-tab" width="100%">
					<tr>
						<th class="tc" width="5%"><input class="allChoose" name=""
							onclick="selAll(this)" type="checkbox"></th>
						<th>编号</th>
						<th>类别名称</th>
						<th>类别描述</th>
						<th>操作</th>
					</tr>
					<c:forEach var="u" items="${categoryList}" varStatus="loop">
						<tr>
							<td class="tc"><input name="id[]" value="${u.categoryId}"
								type="checkbox"></td>
							<td>${u.categoryId }</td>
							<td>${u.categoryName }</td>
							<td>${u.categoryDesc  }</td>
							<td><a class="link-update"
								href="${pageContext.request.contextPath}/manage/admin_to_category_update?id=${u.categoryId}&currentPage=${currentPage}">修改</a>
								<a class="link-del"
								href="javascript:Delete('你确定要删除类别【${u.categoryName} 】吗?', '${pageContext.request.contextPath}/manage/admin_do_category_delete?id=${u.categoryId}&currentPage=${currentPage }')">删除</a>
							</td>
						</tr>
					</c:forEach>

				</table>
				<div class="list-page">
					共 ${totalStudent} 条记录, 当前 ${currentPage} / ${totalPage} 页 <a
						href="admin_do_category_select?currentPage=1${searchParams}">首页</a> <a
						href="admin_do_category_select?currentPage=${currentPage - 1 < 1 ? 1 : currentPage-1}${searchParams}">上一页</a>
					<a
						href="admin_do_category_select?currentPage=${currentPage + 1 > totalPage ? totalPage : currentPage + 1}${searchParams}">下一页</a>
					<a href="admin_do_category_select?currentPage=${totalPage}${searchParams}">尾页</a>
				</div>
			</div>
		</form>
	</div>
</div>
<script src="${pageContext.request.contextPath}/js/function.js"></script>
</body>
</html>

在这里就可以测试分页功能了,测试前需要在数据库中添加资源类别信息,我就不演示了

删除单个资源类别和批量删除资源类别

在controller中的resource包下创建DoCategoryDelete.java,代码如下

@WebServlet("/manage/admin_do_category_delete")
public class DoCategoryDelete extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				/* 设置字符编码,解决中文乱码问题 */
				request.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				/* 接收客户端信息 */
				String id= request.getParameter("id");
				// 创建资源service层接口的实现类
				ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
				/* 调用业务逻辑方法删除资源类别 */
				int count = 0;
				try {
					count = resourceServiceImpl.removeRsCategory(id);
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if(count > 0 ) {
					response.sendRedirect("admin_do_category_select?currentPage="+request.getParameter("currentPage"));
				} else {
					PrintWriter out = response.getWriter();
					out.write("<script>");
					out.write("alert('删除失败')");
					out.write("location.href='admin_do_category_select?currentPage="+request.getParameter("currentPage") + "'");
					out.write("</script>");
				}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/* 设置字符编码,解决中文乱码问题 */
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		/* 接收客户端信息 */
		String ids[]= request.getParameterValues("id[]");
		// 创建资源service层接口的实现类
		ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
		int count = 0;
		/* 遍历资源类别id过程中,调用业务逻辑方法删除资源类别 */
		for(int i = 0; i < ids.length; i++) {
			try {
				count = resourceServiceImpl.removeRsCategory(ids[i]);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		/* 成功或失败重定向到哪里 */
		if(count > 0 ) {
			response.sendRedirect("admin_do_category_select?currentPage=" + request.getParameter("currentPage"));
		} else {
			PrintWriter out = response.getWriter();
			out.write("<script>");
			out.write("alert('批量删除失败')");
			out.write("location.href='admin_do_category_select?currentPage="+request.getParameter("currentPage") + "'");
			out.write("</script>");
		}
	}
}

在service层的ResourceService接口中添加删除资源类别的业务逻辑抽象方法

	// 删除资源类别信息
	public int removeRsCategory(String categoryId) throws SQLException;

在service层的实现类ResourceServiceImpl中重写接口方法,如下

	@Override
	public int removeRsCategory(String categoryId) throws SQLException {
		// 删除资源类别信息
		int i = resourceDao.deleteRsCategory(categoryId);
		return i;
	}

在dao层的ResourceDao接口中添加添加如下抽象方法,执行底层操作,如下

	// 删除资源类别信息
	public int deleteRsCategory(String categoryId) throws SQLException;

在dao层的实现类ResourceDaoImpl中重写接口方法,如下

	@Override
	public int deleteRsCategory(String categoryId) throws SQLException {
		String sql = "delete from category where category_id=?";
		int i = JDBCUtil.executeUpdate(sql, categoryId);
		return i;
	}

同样的,前端删除功能执行效果的js可以使用我们之前的在注册功能写的,这里就可以直接测试了,我就不演示效果了

修改资源类别

获取资源类别信息的servlet

在controller中的resource包下创建ToCategoryUpdate.java,代码如下

@WebServlet("/manage/admin_to_category_update")
public class ToCategoryUpdate extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/* 设置字符编码,解决中文乱码问题 */
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		/* 接收客户端信息 */
		String id = request.getParameter("id");
		//创建资源service层接口的实现类
		ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
		/* 调用业务逻辑方法获取资源类别信息 */
		Category category = null;
		try {
			category = resourceServiceImpl.getRsCategory(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 把资源类别的信息放入请求作用域
		request.setAttribute("category", category);
		// 从请求域获取到的当前页放入请求作用域中
		request.setAttribute("currentPage", request.getParameter("currentPage"));
		// 请求转发到学生信息修改页面
		request.getRequestDispatcher("/WEB-INF/manage/admin_category_modify.jsp").forward(request, response);
	}
}

处理用户修改资源类别信息的servlet
在controller中的resource包下创建DoCategoryUpdate.java,代码如下

@WebServlet("/manage/admin_do_category_update")
public class DoCategoryUpdate extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/* 设置字符编码,解决中文乱码问题 */
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		/* 接收客户端信息 */
		String categoryId = request.getParameter("categoryId");
		String categoryName = request.getParameter("categoryName");
		String categoryDesc = request.getParameter("categoryDesc");
		// 创建资源类别实体
		Category category = new Category(Integer.parseInt(categoryId), categoryName, categoryDesc);
		// 创建资源service层接口的实现类
		ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
		/* 调用业务逻辑方法修改资源类别信息 */
		int count = 0;
		try {
			count = resourceServiceImpl.modifyRsCategory(category);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (count > 0) {
			response.sendRedirect("admin_do_category_select?currentPage=" + request.getParameter("currentPage"));
		} else {
			PrintWriter out = response.getWriter();
			out.write("<script>");
			out.write("alert('用户修改失败')");
			out.write("location.href='admin_to_category_update?id=" + categoryId + "'");
			out.write("</script>");
		}
	}
}

在service层的ResourceService接口中添加如下的业务逻辑抽象方法

	// 获取资源的类别信息
	public Category getRsCategory(String categoryId) throws SQLException;
	
	// 修改资源类别信息
	public int modifyRsCategory(Category category) throws SQLException;

在service层的实现类ResourceServiceImpl中重写接口方法,如下

	@Override
	public Category getRsCategory(String categoryId) throws SQLException {
		// 检索资源的类别信息
		Category category = resourceDao.selectRsCategory(categoryId);
		return category;
	}
	
	@Override
	public int modifyRsCategory(Category category) throws SQLException {
		// 更新资源类别信息
		int i = resourceDao.updateRsCategory(category);
		return i;
	}

在dao层的ResourceDao接口中添加添加如下抽象方法,执行底层操作,如下

	// 更新资源类别信息
	public int updateRsCategory(Category category) throws SQLException;
	
	// 删除资源类别信息
	public int deleteRsCategory(String categoryId) throws SQLException;

在dao层的实现类ResourceDaoImpl中重写接口方法,如下

@Override
	public int updateRsCategory(Category category) throws SQLException {
		String sql = "update category set category_name=?, category_desc=? where category_id = ?";
		int i = JDBCUtil.executeUpdate(sql, category.getCategoryName(), category.getCategoryDesc(), category.getCategoryId());
		return i;
	}
	
	@Override
	public int deleteRsCategory(String categoryId) throws SQLException {
		String sql = "delete from category where category_id=?";
		int i = JDBCUtil.executeUpdate(sql, categoryId);
		return i;
	}

完成到这里就可以测试资源类别修改操作了

添加资源类别

跳转到添加资源类别页面的servlet

在controller中的resource包下创建ToCategoryAdd.java,代码如下

@WebServlet("/manage/admin_to_category_add")
public class ToCategoryAdd extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 请求转发到资源类别信息添加页面
		request.getRequestDispatcher("/WEB-INF/manage/admin_category_add.jsp").forward(request, response);
	}
}

处理用户添加资源类别信息的servlet

在controller中的resource包下创建DoCategoryAdd.java,代码如下

@WebServlet("/manage/admin_do_category_add")
public class DoCategoryAdd extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/* 设置字符编码,解决中文乱码问题 */
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		/* 接收客户端信息 */
		String categoryName = request.getParameter("categoryName");
		String categoryDesc = request.getParameter("categoryDesc");
		// 创建资源service层接口的实现类
		ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
		int count = 0;
		/* 调用业务逻辑方法添加资源类别 */
		try {
			count = resourceServiceImpl.addRsCategory(categoryName, categoryDesc);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (count > 0) {
			response.sendRedirect("admin_do_category_select");
		}else {
			PrintWriter out = response.getWriter();
			out.write("<script>");
			out.write("alert('添加资源类别失败')");
			out.write("</script>");
			out.write("<script>");
			out.write("window.location='" + request.getContextPath() + "/WEB-INF/manage/admin_resource_add.jsp'");
			out.write("</script>");
		}
	}
}

在service层的ResourceService接口中添加如下的业务逻辑抽象方法

	// 添加资源类别
	public int addRsCategory(String categoryName, String categoryDesc) throws SQLException;

在service层的实现类ResourceServiceImpl中重写接口方法,如下

	@Override
	public int addRsCategory(String categoryName, String categoryDesc) throws SQLException {
		//插入资源类别信息
		int i = resourceDao.insertRsCategory(categoryName, categoryDesc);
		return i;
	}

在dao层的ResourceDao接口中添加添加如下抽象方法,执行底层操作,如下

	// 插入资源类别信息
	public int insertRsCategory(String categoryName, String categoryDesc) throws SQLException;

在dao层的实现类ResourceDaoImpl中重写接口方法,如下

	@Override
	public int insertRsCategory(String categoryName, String categoryDesc) throws SQLException {
		String sql = "insert into category (category_name, category_desc) values(?,?)";
		int i = JDBCUtil.executeUpdate(sql, categoryName, categoryDesc);
		return i;
	}

完成到这里就可以测试添加资源类别信息了,这里就不演示了

后台管理已经完成了用户管理,资源管理,现在只剩下论坛管理,下一篇在介绍论坛管理的实现

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-11 17:25:59  更:2021-10-11 17:26:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 18:16:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码