- 资源类别管理
- 获取所有资源类别信息并分页
- 后台资源类别管理页面
- 删除单个资源类别和批量删除资源类别
- 修改资源类别
- 添加资源类别
资源类别管理 获取所有资源类别信息并分页上一篇文章中实现了后台资源管理中的用户资源管理,这一篇介绍资源类别管理的实现
在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 ArrayListgetAllRsCategory(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 ArrayListselectAllRsCategory(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"%>


