栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

⭐基于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");
		
		if (cp != null) {
			currentPage = Integer.parseInt(cp);
		}
		// 创建资源service层接口的实现类
		ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
		
		try {
			array = resourceServiceImpl.getRsCategoryPageTotal(count, keyword);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 调用业务逻辑方法获取所有资源类别信息
		ArrayList 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 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 getAllRsCategory(int currentPage, int count, String keyword) throws SQLException {
		// 检索所有资源的类别信息
		ArrayList listCategory = resourceDao.selectAllRsCategory(currentPage, count, keyword);
		return listCategory;
	}

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

	// 检索所有资源类别总数并算出总页数
	public int[] selectRsCategoryTotal(int count, String keyword) throws SQLException;
	
	// 检索所有资源的资源类别
	public ArrayList 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 selectAllRsCategory(int currentPage, int count, String keyword) throws SQLException {
		ArrayList listCategory = new ArrayList();
		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"%>
首页">>类别管理
关键字:
编号 类别名称 类别描述 操作
${u.categoryId } ${u.categoryName } ${u.categoryDesc } 修改 删除
共 ${totalStudent} 条记录, 当前 ${currentPage} / ${totalPage} 页 首页 上一页 下一页 尾页

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

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

在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("");
				}
	}
	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;
		
		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("");
		}
	}
}

在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("");
		}
	}
}

在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("");
			out.write("");
		}
	}
}

在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;
	}

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号