Javaweb项目案例:一个简单的用户管理系统实现

这篇具有很好参考价值的文章主要介绍了Javaweb项目案例:一个简单的用户管理系统实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.项目背景

我们来设计一个简单的用户管理系统,具有查看用户,添加用户,删除用户,更新用户的所有功能,并能支持分页显示,以及通过关键词模糊查询的

本项目采用Druid数据库连接池

注意:JDBC和DAO部分本文不予演示,请自行完成此部分代码的编写🐿️


2.展示用户列表

模板页面,showuser.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
</head>
<style>
    table {
        width: 80%;
        border-color: white;
    }

    table tr {
        line-height: 30px;
        border-color: white;
    }

    table tr:FIRST-CHILD {
        background: #f2f2f2;
    }

    table tr:nth-child(even) {
        background: #d5eeeb;
    }


</style>
<script type="text/javascript">
    /**
     * 删除用户
     * @param uid 用户ID信息
     */
    function delUser(uid) {
        if (confirm('是否确认删除?')) {
            window.location.href = 'del.do?id=' + uid;
        }
    }

    /**
     * 获取某一页的用户数据
     * @param pageNo 页码
     */
    function page(pageNo) {
        if (pageNo > 0) {
            window.location.href = 'showuser?pageNo=' + pageNo;
        }
    }

</script>
<body>
<form action="showuser" method="post">
    <input type="hidden" name="oper" value="search">
    查找关键字:<input type="text" name="keyword" th:value="${session.keyword}">
    <button type="submit">提 交</button>
</form>
<table>
    <tbody>
    <tr align="center">
        <td>用户名</td>
        <td>学校</td>
        <td>删除内容</td>
        <td>添加内容</td>
    </tr>
    <tr align="center" th:if="${#lists.isEmpty(session.userList)}">
        <td>没有啦</td>
        <td>没有啦</td>
        <td>没有啦</td>
        <td>没有啦</td>
    </tr>

    <tr align="center" th:unless="${#lists.isEmpty(session.userList)}" th:each="user : ${session.userList}">
        <td><a th:text="${user.username}" th:href="@{/edit.do(uid=${user.id})}"></a></td>
        <td><a th:text="${user.school}" th:href="@{/edit.do(uid=${user.id})}"></a></td>
        <td><a th:onclick="|delUser(${user.id})|">删除</a></td>
        <td><a th:href="@{/add.html}">添加</a></td>
    </tr>
    </tbody>
</table>
<!--分页-->
<div>
    <input type="button" value="首 页" th:onclick="|page(1)|">
    <input type="button" value="上一页" th:onclick="|page(${session.pageNo - 1})|">
    <input type="button" value="下一页" th:onclick="|page(${session.pageNo + 1})|">
    <input type="button" value="尾 页" th:onclick="|page(${session.totalPageNo})|">
</div>
</body>
</html>

ShowUserServlet:

/**
 * 使用Thymeleaf渲染页面展示user列表
 */
@WebServlet("/showuser")
public class ShowUserServlet extends ViewBaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        int pageNo = 1;
        // 如果oper为null,说明是通过表单查询按钮点击过来的
        // 如果oper是null,说明不是通过表单查询按钮点击过来的
        String oper = req.getParameter("oper");
        String keyword = null;
        if ("search".equals(oper)) {
            pageNo = 1;
            keyword = req.getParameter("keyword");
            if (keyword == null) {
                keyword = "";
            }
            session.setAttribute("keyword", keyword);
        } else {
            String pageNoStr = req.getParameter("pageNo");
            if (pageNoStr != null) {
                pageNo = Integer.parseInt(pageNoStr);
            }
            Object keywordObj = session.getAttribute("keyword");
            if (keywordObj != null) {
                keyword = (String) keywordObj;
            } else {
                keyword = "";
            }
        }

        // 将页码保存到session作用域
        session.setAttribute("pageNo", pageNo);

        UserDAO userDAO = new UserDAO();
        List<User> userList = userDAO.getUserListByPageAndKeyword(pageNo, keyword);
        // 将userList放到session 作用域
        session.setAttribute("userList", userList);

        // 得到用户总数
        long userCount = userDAO.getUserCount(keyword);
        // 得到页数
        long totalPageNo = userCount / 5 + 1;
        session.setAttribute("totalPageNo", totalPageNo);

        // 注意:物理视图名称 = view-prefix + view-suffix
        // 这里结合xml文件拼接也就是/showuser.html
        super.processTemplate("showuser", req, resp);
    }
}

3.添加用户

add.html

<!-- user表添加页面 -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>用户添加页面</title>
</head>
<style>
  * {
    margin: 0;
    padding: 0;
  }

  /*渐变背景样式*/
  body {
    height: 100vh;
    width: 100%;
    overflow: hidden;
    background-image: linear-gradient(125deg, #F44336, #E91E63, #9C27B0, #3F51B5, #2196F3);
    background-size: 400%;
    font-family: "montserrat";
    animation: bganimation 15s infinite;
  }

  @keyframes bganimation {
    0% {
      background-position: 0% 50%;
    }
    50% {
      background-position: 100% 50%;
    }
    100% {
      background-position: 0% 50%;
    }
  }

  /*表单样式*/
  .form {
    width: 85%;
    max-width: 600px;
    /* max-height:700px;
    */
    background-color: rgba(255, 255, 255, .05);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 40px;
    border-radius: 10px;
    box-shadow: 0 0 5px #000;
    text-align: center;
    font-family: "微软雅黑";
    color: #fff;
  }

  /*表单标题样式*/
  .form h1 {
    margin-top: 0;
    font-weight: 200;
  }

  .form .txtb {
    border: 1px solid #aaa;
    margin: 8px 0;
    padding: 12px 18px;
    border-radius: 10px;
  }

  .txtb label {
    display: block;
    text-align: left;
    color: #fff;
    font-size: 14px;
  }

  /*输入框样式*/
  .txtb input, .txtb textarea {
    width: 100%;
    background: none;
    border: none;
    outline: none;
    margin-top: 6px;
    font-size: 18px;
    color: #fff;
  }

  /*备注框样式*/
  .txtb textarea {
    max-height: 300px;
  }

  /*提交按钮样式*/
  .btn {
    display: block;
    /* background-color:rgba(156,39,176,.5);
    */
    padding: 14px 0;
    color: #fff;
    cursor: pointer;
    margin-top: 8px;
    width: 100%;
    border: 1px solid #aaa;
    border-radius: 10px;
  }
</style>
<body>
<div class="form">
  <form action="add.do" method="post">
    <h1>用户信息添加</h1>
    <div class="txtb">
      <label for="">用户名:</label>
      <input type="text" placeholder="请输入用户名" name="username"
             th:value="${userInfo.username}"></div>
    <div class="txtb">
      <label for="">密码:</label>
      <input type="text" placeholder="请输入密码" name="password"
             th:value="${userInfo.password}"></div>
    <div class="txtb">
      <label for="">学校:</label>
      <input type="text" placeholder="请输入学校" name="school"
             th:value="${userInfo.school}">
    </div>

    <div class="txtb">
      <label for="">备注:</label>
      <textarea name="shit" id=""></textarea>
    </div>
    <input type="submit" value="提 交" class="btn" style="color: #E91E63">
  </form>
</div>
</body>
</html>

AddUserServlet:

/**
 * 向User表中添加数据
 */
@WebServlet("/add.do")
public class AddUserServlet extends ViewBaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String school = req.getParameter("school");
        UserDAO userDAO = new UserDAO();
        userDAO.addUserNoId(username, password, school);
        resp.sendRedirect("showuser");
    }
}

4.删除用户

DelServlet:

/**
 * 根据ID删除用户
 */
@WebServlet("/del.do")
public class DelServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        UserDAO userDAO = new UserDAO();
        userDAO.delUser(id);
        resp.sendRedirect("showuser");
    }
}

5.修改用户

edit.html:

<!-- user表编辑页面 -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户编辑页面</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    /*渐变背景样式*/
    body {
        height: 100vh;
        width: 100%;
        overflow: hidden;
        background-image: linear-gradient(125deg, #F44336, #E91E63, #9C27B0, #3F51B5, #2196F3);
        background-size: 400%;
        font-family: "montserrat";
        animation: bganimation 15s infinite;
    }

    @keyframes bganimation {
        0% {
            background-position: 0% 50%;
        }
        50% {
            background-position: 100% 50%;
        }
        100% {
            background-position: 0% 50%;
        }
    }

    /*表单样式*/
    .form {
        width: 85%;
        max-width: 600px;
        /* max-height:700px;
        */
        background-color: rgba(255, 255, 255, .05);
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        padding: 40px;
        border-radius: 10px;
        box-shadow: 0 0 5px #000;
        text-align: center;
        font-family: "微软雅黑";
        color: #fff;
    }

    /*表单标题样式*/
    .form h1 {
        margin-top: 0;
        font-weight: 200;
    }

    .form .txtb {
        border: 1px solid #aaa;
        margin: 8px 0;
        padding: 12px 18px;
        border-radius: 10px;
    }

    .txtb label {
        display: block;
        text-align: left;
        color: #fff;
        font-size: 14px;
    }

    /*输入框样式*/
    .txtb input, .txtb textarea {
        width: 100%;
        background: none;
        border: none;
        outline: none;
        margin-top: 6px;
        font-size: 18px;
        color: #fff;
    }

    /*备注框样式*/
    .txtb textarea {
        max-height: 300px;
    }

    /*提交按钮样式*/
    .btn {
        display: block;
        /* background-color:rgba(156,39,176,.5);
        */
        padding: 14px 0;
        color: #fff;
        cursor: pointer;
        margin-top: 8px;
        width: 100%;
        border: 1px solid #aaa;
        border-radius: 10px;
    }
</style>
<body>
<div class="form">
    <form th:action="@{/update.do}" method="post">
        <h1>用户信息修改</h1>
        <!--隐藏传递用户ID信息,使用隐藏域-->
        <input type="hidden" name="id" th:value="${userInfo.id}">
        <div class="txtb">
            <label for="">用户名:</label>
            <input type="text" placeholder="请输入用户名" name="username"
                   th:value="${userInfo.username}"></div>
        <div class="txtb">
            <label for="">密码:</label>
            <input type="text" placeholder="请输入密码" name="password"
                   th:value="${userInfo.password}"></div>
        <div class="txtb">
            <label for="">学校:</label>
            <input type="text" placeholder="请输入学校" name="school"
                   th:value="${userInfo.school}">
        </div>

        <div class="txtb">
            <label for="">备注:</label>
            <textarea name="shit" id=""></textarea>
        </div>
        <input type="submit" value="提 交" class="btn" style="color: #E91E63">
    </form>
</div>
</body>
</html>

EditServlet:

/**
 * 编辑user表中的数据
 */
@WebServlet("/edit.do")
public class EditServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uidStr = req.getParameter("uid");
        if (uidStr != null && !"".equals(uidStr)) {
            int uid = Integer.parseInt(uidStr);
            UserDAO userDAO = new UserDAO();
            User user = userDAO.getUserById(uid);
            // 保存到request作用域
            req.setAttribute("userInfo", user);
            super.processTemplate("edit", req, resp);
        }
    }
}

6.界面展示

展示用户列表:(此页面支持分页,模糊查询,修改,删除,添加)🦫

Javaweb项目案例:一个简单的用户管理系统实现

用户信息修改:(用户信息添加类似)🦎

Javaweb项目案例:一个简单的用户管理系统实现文章来源地址https://www.toymoban.com/news/detail-489144.html

到了这里,关于Javaweb项目案例:一个简单的用户管理系统实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JavaWeb综合案例——商品后台管理系统

    目录 1.功能介绍 2.工程准备 2.1pom.xml 2.2mybatis-config.xml 2.3SqlSessionFactoryUtils 2.4CheckCodeUtil 3.注册页面 3.1User 3.2UserMapper.xml 3.3UserMapper 3.4UserService 3.5register.html 3.6RegisterServlet 3.7CheckCodeServlet 4.登录页面 4.1login.html 4.2LoginServlet 4.3LoginFilter 5.后台主页面 5.1Brand 5.2BrandMapper 5.3BrandMapper.xml

    2024年02月07日
    浏览(31)
  • 一个简单的vue项目之图书管理系统,自用,无ui,持续更新...

    由于自己上一把忘记写log导致不小心把我的前端项目删了 重新随便写点log记录一下 由于各种版本不适配问题,请大家谨慎 看攻略 参考。 另外,由于博主主要还是写后端,所以对ui并没有加以处理,进阶就不需要参考了!但是博主还是很乐于学习的,如果有什么好的意见和建

    2024年02月06日
    浏览(31)
  • SpringBoot小项目——简单的小区物业后台管理系统 & 认证鉴权 用户-角色模型 & AOP切面日志 & 全局异常【源码】

    基于SpringBoot的简单的小区物业后台管理系统,主要功能有报修的处理,楼宇信息和房屋信息的管理,业主信息的管理【核心】,以及数据统计分析模块Echarts绘图;此外采用用户-角色权限模型,结合自定义注解实现简单的权限管理功能,采用aop切面实现日志的存储,全局异常

    2024年02月06日
    浏览(39)
  • JavaWeb:(练习)十二、简单的学生管理系统

    ​ 基于练习八、练习九、练习十、练习十一的逐步练习基础上,实现了一个简单的学生管理系统。 ​ 练习八链接:JavaWeb:(练习)八、Servlet前端发送数据到后端练习 ​ 练习九链接:JavaWeb:(练习)九、Servlet数据交互、XMLHttpRequest、JSON、AJAX、AXIOS练习 ​ 练习十链接:J

    2024年02月09日
    浏览(31)
  • JavaWeb阶段案例--简易版管理图书系统(增删改查)

      在WEB-INF下创建一个lib包,并导入以下jar包:(导入后,鼠标右键选中lib文件夹,单击“add as lib...”  注:使用Lombok包时,除了需要导入jar包还需要从idea中下载Lombok插件 file -- setting -- plugins -- 搜Lombok -- 勾选上 -- Apply -- OK file -- setting -- Build Exec.....  -- Compiler -- Annotation Pro

    2024年02月08日
    浏览(31)
  • 【新手级】JavaWeb用户管理系统—使用JSP, Servlet, JDBC, MySQL

    这是我学完JavaWeb后做的期末大作业,是一个用户管理系统,包括登录注册功能,对于列表的增、删、改、查功能,由于我也是参考的网上大佬的的代码,之后进行了一些修改,完成的这个新手项目,于是我也把这个项目源码放在这里供大家参考,同时也对这次学习做一个记录

    2024年02月07日
    浏览(35)
  • JavaWeb期末项目 图书馆管理系统

    1 项目基本信息 1.1 项目名称 图书馆管理系统 1.2 开发运行环境 Window 10 64位 JDK 1.8.0 Eclipse 4.8版本 MySql 5.5 Tomcat 9.0 2 项目需求分析 2.1 学生登录部分 (1)学生注册:在进入图书馆前必须要登录,如果没有学号则要注册,注册时系统会将用户填写的学号与数据库里面的数据对比,

    2024年02月10日
    浏览(33)
  • JAVAWEB学生信息管理系统保姆级教程(增删改查+<普通用户和管理员>登录注册+Filter+mysql+批量删除信息+用户退出登录注销)eclipse版(升级版)

    该项目源码地址: 源码地址请点击这里哟!         AdminBean.java        对数据库里的用户名的表的数据进行封装。         StudentBean.java         对数据库里的学生信息的表的数据进行封装。         AdminDao.java         实现登录和注册的方法。         

    2024年02月08日
    浏览(50)
  • JavaWeb项目:航班信息管理系统(tomcat+jsp)

    航班信息管理系统是学习Javaweb的一个小项目,首先对该项目的业务需求进行分析,根据项目文档知它的主要实现技术为 SERVLET、JSP、MVC 架构、JDBC 和 MySQL。该项目着重学生的实际应用场景来设计,模拟 机场中的航班系统的业务实现以及扩展,能够实现航班信息管理的的所有功

    2024年04月12日
    浏览(28)
  • 超适合练手的一套JavaWeb项目 (超市后台管理系统)

    1.搭建一个maven web项目 2.配置Tomcat 3.测试项目是否能够跑起来 4.导入项目中遇到的jar包 5.创建项目结构 1.数据库配置文件 db.properties文件代码 2.编写数据库的公共类 java代码 3.编写字符编码过滤器 xml代码 java dao层接口代码 java dao接口的实现类代码 java service接口代码

    2024年02月05日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包