需求
- 根据按钮点击修改之后,跳转修改页面,且回显原邮箱信息.
- 点击保存,更改邮箱信息.
分析
- jsp
- 点击之后携带id跳转到回显的servlet
- 修改jsp,点击保存,请求更新servlet
- servlet
- 回显servlet
- 设置响应编码
- 取到页面id
- 根据id调用service的findByIdEmail(id)方法,返回email对象
- 将email存入request域,跳转到updateEmail.jsp页面
- 更新servlet
- 设置请求编码
- 获取页面所有值,封装为map
- 封装对象,调用BeanUtils.populate
- 调用service的updateByIdEmail(email)方法
- 重定向到show页面
- 回显servlet
- service
- 回显
将id转为int型
调用dao的findByIdEmail(id)方法,返回Email对象 - 更新
调用dao的updateEmail(email)方法
- 回显
- dao
- 回显
定义SQL
调用template.queryForObject方法,返回Email对象 - 更新
定义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> 退出</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());
}