⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台资源管理demo2

Posted stormzhuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了⭐基于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();
				基于内容与基于协作的过滤?

为啥 Kafka 是基于拉的而不是基于推的?

推荐算法简介:基于用户的协同过滤基于物品的协同过滤基于内容的推荐

依存句法分析:基于图的依存句法分析基于转移的依存句法分析基于神经网络的依存句法分析

基于项目和基于内容的协同过滤有啥区别?

事件处理:基于功能的组件与基于类的组件