JavaWeb小工程实战演练(四)

用户信息删除

1. 网页设计在这里插入图片描述
还是这样的一个页面

2. 后台代码实现

删除Servlet:DelServletById

package cn.hehewocao.Servlet;

import cn.hehewocao.Service.UserService;
import cn.hehewocao.Service.UserServiceImp;

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;
import java.io.UnsupportedEncodingException;

@WebServlet("/delServletById")
public class DelServletById extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("delId");
        UserService us = new UserServiceImp();
        us.delUserById(id);
        resp.sendRedirect("/findServletByPage");
    }
}

UserServiceImp类添加方法:

  @Override
    public void delUserById(String _id) {
        UserDao ud = new UserDaoImp();
        int id = Integer.parseInt(_id);
        ud.delUserById(id);
    }

UserDaoImpl类添加方法:

 @Override
    public void delUserById(int id) {
        String sql = "delete from user where id = ? ";
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDateSource());
        jdbcTemplate.update(sql, id);
    }

3. JSP页面设计

JSP页面中主要实现将用户的id和与之对应删除按钮绑定在一起

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>用户管理</title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->

    <style>

        tr, th, td {
            text-align: center;
        }

    </style>

</head>
<body>


<div style="width: 70%;margin-left: 15%;margin-top: 50px;" align="center">
    <form class="form-inline" action="${pageContext.request.contextPath}/findServletByPage" method="post">
        <div class="form-group">
            <label>编号</label>
            <input id="id" type="text" name="id" value="${condition.id[0]}" placeholder="输入编号" align="center"/>
        </div>
        <div class="form-group">
            <label>用户名</label>
            <input id="username" type="text" value="${condition.username[0]}" name="username" placeholder="输入用户名" align="center"/>
        </div>
        <div class="form-group">
            <label>昵称</label>
            <input type="text" id="nickname" value="${condition.nickname[0]}" name="nickname" placeholder="昵称" align="center"/>
        </div>

        <div class="form-group">
            <label>地址</label>
            <input type="text" id="address" value="${condition.address[0]}" name="address" placeholder="地址" align="center"/>
        </div>

        <button type="submit" class="btn btn-default" id="find">查询</button>&nbsp;&nbsp;&nbsp;
        <a  href="register.jsp"><button type="button" class="btn btn-primary">添加用户</button></a>
    </form>
</div>


<div style="width: 80%;margin-left: 10%;margin-top: 50px;">
    <table class="table table-hover">
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>昵称</th>
            <th>性别</th>
            <th>生日</th>
            <th>手机号</th>
            <th>邮箱</th>
            <th>地址</th>
            <th>职业</th>
            <th>描述</th>
            <th colspan="2">操作</th>
        </tr>

        <c:if test="${not empty pageBean}">

            <c:forEach items="${pageBean.list}" var="user" begin="0" end="${pageBean.list.size() - 1}" step="1"
                       varStatus="s">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.username}</td>
                    <td>${user.password}</td>
                    <td>${user.nickname}</td>
                    <td>${user.sex}</td>
                    <td>${user.birthday}</td>
                    <td>${user.phone}</td>
                    <td>${user.email}</td>
                    <td>${user.address}</td>
                    <td>${user.occupation}</td>
                    <td>${user.describes}</td>
                    <td><a href="${pageContext.request.contextPath}/updateUserForwordServlet?beforeUserId=${user.id}" name="del" id="update${s}">修改</a></td>
                    <td><a href="${pageContext.request.contextPath}/delServletById?delId=${user.id}" name="del" id="${s}"
                           onclick="delLine(this)">删除</a></td>	<%--将删除链接绑定用户id--%>
                </tr>
            </c:forEach>
        </c:if>
    </table>
</div>


<%--分页栏--%>
<div align="center" style="margin-top: 50px;">
    <nav aria-label="Page navigation">
        <ul class="pagination">


            <c:if test="${pageBean.page == 1}">
                <li class="disabled">
                    <a href="#"
                       aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
            </c:if>
            <c:if test="${pageBean.page != 1}">
                <li>
                    <a href="${pageContext.request.contextPath}/findServletByPage?page=${pageBean.page - 1}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&address=${condition.address[0]}"
                       aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
            </c:if>


            <c:forEach begin="1" end='${pageContext.request.getAttribute("pageBean").pages}' var="i">
                <c:if test="${pageBean.page == i}">
                    <li class="active"><a
                            href='${pageContext.request.contextPath}/findServletByPage?page=${i}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&address=${condition.address[0]}'>${i}</a></li>
                </c:if>
                <c:if test="${pageBean.page != i}">
                    <li><a href='${pageContext.request.contextPath}/findServletByPage?page=${i}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&address=${condition.address[0]}'>${i}</a></li>
                </c:if>
            </c:forEach>


            <c:if test="${pageBean.pages == pageBean.page}">
                <li class="disabled">
                    <a href="#"
                       aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>

            <c:if test="${pageBean.pages != pageBean.page}">
                <li>
                    <a href="${pageContext.request.contextPath}/findServletByPage?page=${pageBean.page + 1}&rows=5&id=${condition.id[0]}&username=${condition.username[0]}&nickname=${condition.nickname[0]}&$address=${condition.address[0]}"
                       aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>

        </ul>
    </nav>
    <p style="font-weight: bold">
        共${pageBean.count == null ? 0 : pageBean.count}条记录,共${pageBean.pages == null ? 0 : pageBean.pages}页</p>
</div>


<script>
    function delLine(obj) {
        if (confirm("确认删除?")) {
            return true;
        }
    }


</script>


<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>

至此删除功能就被实现了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值