客户信息管理系统7—客户信息的修改
4、功能四:客户信息的修改:
(1)修改流程
(2)修改分析
选择要修改的客户信息,然后把要修改的信息显示在表单上;然后在该表单上进行修改信息,然后点击修改完成,然后重新显示修改后的客户查询信息情况
(3)代码组成
findAllCustomer.jsp+ updateInfo.jsp+
GetUpdateInfoServlet+UpdateServlet +
CustomersService + CustomersDao + CustomersDaoImplement
(4)代码功能介绍
【1】findAllCustomer.jsp:查询结果显示页面,修改客户信息的入口
【2】updateInfo.jsp :显示要修改的表单信息页面
【3】GetUpdateInfoServlet:获取要修改的客户信息的web层
【4】UpdateServlet:修改客户信息的web层
【5】CustomersService :业务层(getUpdateInfo(id)方法,update(customers)方法)
【6】CustomersDao:dao接口层(getUpdateInfo(id)方法,update(customers)方法)
【7】CustomersDaoImplement:dao结构层实现类(getUpdateInfo(id)方法,update(customers)方法)
(5)代码详细
4.5.1 findAllCustomer.jsp:查询结果显示页面,修改客户信息的入口
详细看功能一代码实现,已经写好了
4.5.2 updateInfo.jsp :显示要修改的表单信息页面
<%@ page language="java" import="java.util.*"
contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'addCustomer.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- 路径问题:此种是错误的 -->
<!-- <script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script> -->
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<link href="css/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
$(function(){
$("#birthday").datepicker({
minDate: new Date(1980, 0, 01),
maxDate: new Date(2020, 11, 31),
yearRange :'-40:+30',
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd'
});
});
</script>
</head>
<h3>客户信息修改的页面</h3>
<body>
<!-- <font color="red">${msg }</font> -->
<%
//生成一个唯一的串
String token = UUID.randomUUID().toString();
session.setAttribute("token_session", token);//设置session
%>
<form action="${pageContext.request.contextPath }/UpdateServlet"
method="post">
<!-- <form action="/customer_system/CustomersServlet" method="post"> -->
<table>
<tr>
<td>编号</td>
<td>
${customers.id }
</td>
</tr>
<tr>
<td>名字</td>
<td>
<input type="text" name="name" value="${customers.name }">
<!-- 设置token 的隐藏数值 -->
<input type="hidden" name="token" value="${token_session }">
<input type="hidden" name="id" value="${customers.id }">
</td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="gender" value="男"
<c:if test="${customers.gender=='男'}">
checked="checked"
</c:if>
>男
<input type="radio" name="gender" value="女"
<c:if test="${customers.gender=='女'}">
checked="checked"
</c:if>
>女</td>
</tr>
<tr>
<td>生日</td>
<td><input type="text" name="birthday" id="birthday" value="${customers.birthday }"></td>
</tr>
<tr>
<td>手机</td>
<td><input type="text" name="cellphone" value="${customers.cellphone }"></td>
</tr>
<tr>
<td>电子邮件</td>
<td><input type="text" name="email" value="${customers.email }"></td>
</tr>
<tr>
<td>客户爱好</td>
<!-- 错误的逻辑 -->
<!-- <c:if test="${customers.preference=='上班'}"> -->
<!-- checked="checked" -->
<!-- </c:if> -->
<td><input type="checkbox" name="preference" value="上班"
<c:if test="${fn:contains(customers.preference,'上班')}">
checked="checked"
</c:if>
>上班
<input type="checkbox" name="preference" value="工作"
<c:if test="${fn:contains(customers.preference,'工作')}">
checked="checked"
</c:if>
>工作
<input type="checkbox" name="preference" value="学习"
<c:if test="${fn:contains(customers.preference,'学习')}">
checked="checked"
</c:if>
>学习
<input type="checkbox" name="preference" value="睡觉"
<c:if test="${fn:contains(customers.preference,'睡觉')}">
checked="checked"
</c:if>
>睡觉</td>
</tr>
<tr>
<td>客户类型</td>
<td><select name="type">
<option value="vip"
<c:if test="${customers.type=='vip'}">
selected="selected"
</c:if>
>vip</option>
<option value="黄金vip"
<c:if test="${customers.type=='黄金vip'}">
selected="selected"
</c:if>
>黄金vip</option>
<option value="普通用户"
<c:if test="${customers.type=='普通用户'}">
selected="selected"
</c:if>
>普通用户</option>
<option value="超级vip"
<c:if test="${customers.type=='超级vip'}">
selected="selected"
</c:if>
>超级vip</option>
</select></td>
</tr>
<tr>
<td>备注</td>
<!-- <td><textarea rows="5" cols="30" name="description" value="${customers.description }"></textarea></td> -->
<td><textarea rows="5" cols="30" name="description" >${customers.description}</textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交">
<input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
4.5.3 GetUpdateInfoServlet:获取要修改的客户信息的web层
package com.zhku.jsj144.zk.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhku.jsj144.zk.domain.Customers;
import com.zhku.jsj144.zk.service.CustomersService;
public class GetUpdateInfoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到修改的相关信息
String id = request.getParameter("id");
CustomersService customersService=new CustomersService();
Customers customers=customersService.getUpdateInfo(id);
//保存查询结果,并将查询结果显示到修改信息页面进行显示
request.setAttribute("customers", customers);
request.getRequestDispatcher("/updateInfo.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.5.4 UpdateServlet:修改客户信息的web层
package com.zhku.jsj144.zk.web;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.zhku.jsj144.zk.domain.Customers;
import com.zhku.jsj144.zk.service.CustomersService;
public class UpdateServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("text/html;charset=utf-8");
//更新要修改的表单信息
CustomersService customersService=new CustomersService();
//封装表单信息
try {
Customers customers=new Customers();
BeanUtils.populate(customers, request.getParameterMap());
//额外处理
//对于爱好preference是多个值,那么使用beanutils封装时,会丢失数据,需要手动处理
String[] values=request.getParameterValues("preference");
if(values!=null){
String preference=Arrays.toString(values);//将数组变为字符串[上班,工作]
//进一步处理:将 [上班,工作] ---> 上班,工作
preference=preference.substring(1, preference.length()-1);
customers.setPreference(preference);//设置爱好
}
//更改信息
customersService.update(customers);
//重新显示查询信息
request.getRequestDispatcher("/FindAllServlet").forward(request, response);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.5.5 CustomersService :业务层(getUpdateInfo(id)方法,update(customers)方法)
详细看功能一代码实现,已经写好了
4.5.6 CustomersDao:dao接口层(getUpdateInfo(id)方法,update(customers)方法)
详细看功能一代码实现,已经写好了
4.5.7 CustomersDaoImplement:dao结构层实现类(getUpdateInfo(id)方法,update(customers)方法)
详细看功能一代码实现,已经写好了
(6)表单信息回显分析
客户信息的回显 :
【1】第一类: 直接通过el 表达式取值
【2】第二类:对于radio的回显
设置checked=”chceked”,不过加上条件去判断是否要进行设置
【3】第三类:checkbox的回显
设置checked=”chceked”,不过加上条件去判断是否要进行设置
【4】第四类:下拉框
设置selected=”selected”,不过加上条件去判断是否要进行设置