JavaWeb之更新一条数据,显示到页面

本文介绍了一个JavaWeb应用中更新数据的过程。首先,根据按钮点击,携带ID跳转到回显页面显示原有邮箱信息。接着,用户保存更改后,请求更新Servlet,通过Service和DAO层实现数据更新,并重定向到展示页面。整个流程涉及JSP、Servlet、Service和DAO的交互,实现了数据的修改与回显。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求

  1. 根据按钮点击修改之后,跳转修改页面,且回显原邮箱信息.
  2. 点击保存,更改邮箱信息.

分析

  1. jsp
    1. 点击之后携带id跳转到回显的servlet
    2. 修改jsp,点击保存,请求更新servlet
  2. servlet
    1. 回显servlet
      1. 设置响应编码
      2. 取到页面id
      3. 根据id调用service的findByIdEmail(id)方法,返回email对象
      4. 将email存入request域,跳转到updateEmail.jsp页面
    2. 更新servlet
      1. 设置请求编码
      2. 获取页面所有值,封装为map
      3. 封装对象,调用BeanUtils.populate
      4. 调用service的updateByIdEmail(email)方法
      5. 重定向到show页面
  3. service
    1. 回显
      将id转为int型
      调用dao的findByIdEmail(id)方法,返回Email对象
    2. 更新
      调用dao的updateEmail(email)方法
  4. dao
    1. 回显
      定义SQL
      调用template.queryForObject方法,返回Email对象
    2. 更新
      定义SQL
      调用template.update方法

代码实现

jsp页面

       <a href="#">
            <span class="glyphicon glyphicon-edit" οnclick="updateEmail(${email.id})"></span>
        </a>
//更新一条记录,获取id
    function updateEmail(id){
        location.href="${pageContext.request.contextPath}/email/showByIdEmail?id="+id;
    }

updateEmail页面

<!doctype html>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<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 rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css" >
    <style>
        . {
            margin: 0;
            padding: 0;
        }
    </style>

</head>
<body>
<!--导航条-->
<div style="margin-bottom: -30px;">
    <nav class="navbar navbar-inverse" >
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">行情来了管理系统</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li ><a href="#">会员管理 <span class="sr-only">(current)</span></a></li>
                    <li class="active"><a href="#">邮箱管理</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="#">
                            ${session_name}
                        </a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-log-in" aria-hidden="true"></span>&nbsp;&nbsp;&nbsp;退出</a></li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
</div>
<!--巨幕-->
<div class="jumbotron">
    <div class="container">
        <h2>修改邮箱信息表</h2>
    </div>
</div>
<%--主体--%>
<div class="container">
    <form class="form-horizontal" action="${pageContext.request.contextPath}/email/updateByIdEmail" method="post">
        <input type="hidden" value="${email.id}" name="id">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">邮箱名称</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="email_name" id="inputEmail3" value="${email.email_name}" readonly>
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">邮箱账户</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="email_pass" value="${email.email_pass}" id="inputPassword3">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">邮箱类型</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputPassword4" name="email_type" value="${email.email_type}">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">是否正常使用</label>
            <div class="col-sm-10">
                <div class="radio">
                    <label>
                        <input type="radio" name="is_use" id="optionsRadios1" value="yes" checked>Yes
                    </label>
                </div>
                <div class="radio">
                    <label>
                        <input type="radio" name="is_use" id="optionsRadios2" value="no">No
                    </label>
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-success">保存</button>
            </div>
        </div>
    </form>
</div>

<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js" ></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
</body>
</html>

EmailServlet

//回显页面
    public void showByIdEmail(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //1.设置响应编码和请求编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //2.获取id值
        String id = request.getParameter("id");
        //3.根据id值返回email对象,回显到界面
        Email email = service.findByIdEmail(id);
        request.setAttribute("email",email);
        request.getRequestDispatcher("/updateEmail.jsp").forward(request,response);
    }
    //根据id修改一条记录
    public void updateByIdEmail(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        //1.设置请求编码
        request.setCharacterEncoding("utf-8");
        //2.获取页面请求值
        Map<String, String[]> map = request.getParameterMap();
        //3.封装对象
        Email email = new Email();
        BeanUtils.populate(email,map);
        //4.调用service的updateByIdEmail方法
        service.updateByIdEmail(email);
        //5.重定向到show
        response.sendRedirect(request.getContextPath()+"/email/show");
    }

EmailService

@Override
    public Email findByIdEmail(String eid) {
        //id转为int型
        int id = Integer.parseInt(eid);
        return dao.findByIdEmail(id);
    }

    @Override
    public void updateByIdEmail(Email email) {
        dao.updateEmail(email);
    }

EmailDao

@Override
    public Email findByIdEmail(int id) {
        //1.定义sql
        String sql = "select * from send_email where id=?";
        //2.调用template的queryForObject方法
        Email email = template.queryForObject(sql, new BeanPropertyRowMapper<Email>(Email.class), id);
        return email;
    }

    @Override
    public void updateEmail(Email email) {
        //1.定义sql语句
        String sql = "update send_email set email_name=?,email_pass=?,email_type=?,is_use=? where id = ?";
        //2.调用template的queryForObject方法
        template.update(sql,email.getEmail_name(),email.getEmail_pass(),email.getEmail_type(),email.getIs_use(),email.getId());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值