欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

管理员添加功能———删除用户,用户信息分页(jsp)

程序员文章站 2022-04-26 10:00:48
...

删除用户

管理员添加功能———删除用户,用户信息分页(jsp)
从首页跳转到删除页面写删除用户jsp页面
管理员添加功能———删除用户,用户信息分页(jsp)
写删除用户jsp页面,使用a标签跳转到处理页面
管理员添加功能———删除用户,用户信息分页(jsp)

<%@ page import="net.lmq.shop.service.UserService" %>
<%@ page import="net.lmq.shop.bean.User" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
       value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
    <title>显示用户信息</title>
    <base href="${basePath}">
    <link href="styles/css/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<hr>
<table width="90%" border="0px">
    <tr>
        <td align="left">登录用户:<span style="color: red;">${username}</span></td>
        <td align="right"><a href="logout" target="_parent">注销</a></td>
    </tr>
</table>
<hr>
<h3>用户列表</h3>
<hr>
<%
    //创建用户服务对象
    UserService userService = new UserService();
    //获取全部用户
    List<User> users = userService.findAllUsers();
%>
<table border="1" width="90%" cellspacing="0">
    <tr>
        <th>编号</th>
        <th>用户名</th>
        <th>是否删除</th>
    </tr>
    <c:forEach var="user" items="<%=users%>">
        <tr align="center">
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td><a href="deleteUser?id=${user.id}">删除</a></td>
        </tr>
    </c:forEach>
</table>
<hr>
</body>
</html>

管理员添加功能———删除用户,用户信息分页(jsp)
删除servlet,完成后跳转到删除页面
管理员添加功能———删除用户,用户信息分页(jsp)

package net.lmq.shop.servlet;

import net.lmq.shop.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteUser")
public class deleteUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //得到用户编号
        int id = Integer.parseInt(request.getParameter("id"));
        //创建用户服务对象
        UserService userService= new UserService();
        int count =userService.deleteUserById(id);
        if(count>0){
            //重定向跳转
            response.sendRedirect(request.getContextPath()+"/backend/deteleUser.jsp");
        }else {
            response.sendRedirect(request.getContextPath()+"/backend/deteleUser.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

实现用户信息分页
管理员添加功能———删除用户,用户信息分页(jsp)
在用户显示页面添加代码,
管理员添加功能———删除用户,用户信息分页(jsp)

<%--//实现分页操作【首页、上一页、下一页、末页--%>
<p style="text-align: center">
    <a href="showUser?pageIndex=1">[首页]</a>&nbsp;&nbsp;
    <a href="showUser?pageIndex=<%= Integer.parseInt(request.getParameter("pageIndex"))-1%>">[上一页]</a>&nbsp;&nbsp;
    <a href="showUser?pageIndex=<%= Integer.parseInt(request.getParameter("pageIndex"))+1%>">[下一页]</a>&nbsp;&nbsp;
    <a href="showUser?pageIndex=<%= Integer.parseInt(request.getParameter("totalPage"))%>">[末页]</a>&nbsp;&nbsp;
    <%=request.getParameter("pageIndex")%>/ <%=request.getParameter("totalPage")%>
</p>

在显示用户处理页面servlet改代码,得到总页数(用户数量 / 没有显示数),得到当前页数(判断:1或传入的),得到新的用户列表

/**
 * 功能:生成订单控制程序
 * 作者:赖明庆
 * 日期:2019.12.18
 */
package net.lmq.shop.servlet;

import net.lmq.shop.bean.User;
import net.lmq.shop.service.UserService;
import org.omg.PortableInterceptor.INACTIVE;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/showUser")
public class ShowUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建用户服务对象
        UserService userService = new UserService();
        //获取全部用户
        List<User> users = userService.findAllUsers();

        //总页数
        int totalPage = users.size() % 5 ==0 ? users.size()/5 :users.size()/5 +1;
        //得到当前页数
        int pageIndex =request.getParameter("pageIndex") == null? 1: Integer.parseInt(request.getParameter("pageIndex"));
        //限制首页和末页
        if(pageIndex <1){
            pageIndex = 1;
        }else if(pageIndex > totalPage){
            pageIndex = totalPage;
        }

        //创建放用户的新列表
        List<User> users1 = new ArrayList<>();
        //得到页面要显示的用户
        for(int  i = (pageIndex -1)*5;i<pageIndex*5;i++){
            if(i<users.size()){
                users1.add(users.get(i));
            }
        }

        //获取session对象
        HttpSession session = request.getSession();
        session.setAttribute("users",users1);
        //重定向到显示用户页面
        response.sendRedirect(request.getContextPath()+"/backend/showUser.jsp?pageIndex="+pageIndex+"&totalPage="+totalPage);
        for(User user:users){
            System.out.println(user);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

删除页面实现分页,使用硬分页,

<%@ page import="net.lmq.shop.service.UserService" %>
<%@ page import="net.lmq.shop.bean.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
       value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
    <title>显示用户信息</title>
    <base href="${basePath}">
    <link href="styles/css/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<hr>
<table width="90%" border="0px">
    <tr>
        <td align="left">登录用户:<span style="color: red;">${username}</span></td>
        <td align="right"><a href="logout" target="_parent">注销</a></td>
    </tr>
</table>
<hr>
<h3>用户列表</h3>
<hr>
<%
    //创建用户服务对象
    UserService userService = new UserService();
    //获取全部用户
    List<User> users = userService.findAllUsers();

    //总页数
    int totalPage = users.size() % 5 ==0 ? users.size()/5 :users.size()/5 +1;
    //得到当前页数
    int pageIndex =request.getParameter("pageIndex") == null? 1: Integer.parseInt(request.getParameter("pageIndex"));
    //限制首页和末页
    if(pageIndex <1){
        pageIndex = 1;
    }else if(pageIndex > totalPage){
        pageIndex = totalPage;
    }

    //创建放用户的新列表
    List<User> users1 = new ArrayList<>();
    //得到页面要显示的用户
    for(int  i = (pageIndex -1)*5;i<pageIndex*5;i++){
        if(i<users.size()){
            users1.add(users.get(i));
        }
    }

%>
<table border="1" width="90%" cellspacing="0">
    <tr>
        <th>编号</th>
        <th>用户名</th>
        <th>是否删除</th>
    </tr>
    <c:forEach var="user" items="<%=users1%>">
        <tr align="center">
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td><a href="deleteUser?id=${user.id}">删除</a></td>
        </tr>
    </c:forEach>
</table>
<hr>
<p style="text-align: center">
    <a href="backend/deteleUser.jsp?pageIndex=1">[首页]</a>&nbsp;&nbsp;
    <a href="backend/deteleUser.jsp?pageIndex=<%= pageIndex-1%>">[上一页]</a>&nbsp;&nbsp;
    <a href="backend/deteleUser.jsp?pageIndex=<%= pageIndex+1%>">[下一页]</a>&nbsp;&nbsp;
    <a href="backend/deteleUser.jsp?pageIndex=<%= totalPage%>">[末页]</a>&nbsp;&nbsp;
    <%= pageIndex%>/ <%=totalPage%>
</p>
</body>
</html>

相关标签: jsp