上一篇文章中实现了后台资源管理中的用户资源管理,这一篇介绍资源类别管理的实现
资源类别管理
获取所有资源类别信息并分页
在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;
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);
}
ResourceServiceImpl resourceServiceImpl = new ResourceServiceImpl();
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 = "";
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">></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}¤tPage=${currentPage}">修改</a>
<a class="link-del"
href="javascript:Delete('你确定要删除类别【${u.categoryName} 】吗?', '${pageContext.request.contextPath}/manage/admin_do_category_delete?id=${u.categoryId}¤tPage=${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");
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[]");
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("<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");
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);
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");
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;
}
完成到这里就可以测试添加资源类别信息了,这里就不演示了
后台管理已经完成了用户管理,资源管理,现在只剩下论坛管理,下一篇在介绍论坛管理的实现
|