JavaWeb--MVC案例1-------(6)修改
修改:
先显示(SELECT 操作)修改的页面,再进行修改(update)
显示修改页面
Update 的超链接:<a href="edit.do?id=<%= customer.getId() %>">UPDATE</a>
edit 方法: 参考注释
JSP 页面:
获取请求域中的 Customer 对象,调用对应的字段的 get 方法来显示值。
使用隐藏域来保存要修改的 Customer 对象的 id:<input type="hidden" name="id" value=“<%= customer.getId() %>"/>
使用隐藏域来保存 oldName:<input type="hidden" name=“oldName" value=“<%= customer.getName() %>"/>
关于隐藏域:和其他的表单域一样可以被提交到服务器,只不过在页面上不显示
提交到 update.do
edit方法
private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String id = req.getParameter("id");
String forwardPath = "error.jsp";
//调用customerDAO的get(int id)获取和id对应的Customer对象customer
Customer customer = customerDAO.get(Integer.parseInt(id));
if(customer != null){
forwardPath = "/updateCustomer.jsp";
req.setAttribute("customer", customer);
}
req.getRequestDispatcher(forwardPath).forward(req, resp);
}
然后到updateCustomer.jsp中
<%@ page import="mvccases.Customer" %><%--
Created by IntelliJ IDEA.
User: Skye
Date: 2017/12/11
Time: 15:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message != null){
%>
<br>
<font color="red"><%= message%></font>
<br>
<br>
<%
}
String id = null;
String name = null;
String address = null;
String phone = null;
String oldName = null;
Customer customer = (Customer)request.getAttribute("customer");
if(customer != null){
id = customer.getId() + "";
name = customer.getName();
oldName = customer.getName();
address = customer.getAddress();
phone = customer.getPhone();
} else{
id = request.getParameter("id");
name = request.getParameter("name");
oldName = request.getParameter("oldName");
address = request.getParameter("address");
phone = request.getParameter("phone");
}
%>
<form action="update.do" method="post">
<%--隐藏域 --%>
<input type="hidden" name="id" value="<%= id%>"/>
<input type="hidden" name="oldName" value="<%= oldName%>"/>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="<%= name%>"/></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" value="<%= address%>"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" value="<%= phone%>"/></td>
</tr>
<tr>
<td><input type="submit" name="Submit"/></td>
</tr>
<%-- <form>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<tr>
<td><%=customer.getId()%></td>
<td><%=customer.getName()%></td>
<td><%=customer.getAddress()%></td>
<td><%=customer.getPhone()%></td>
</tr>
</form>--%>
</table>
</form>
</body>
</html>
然后使用update()方法更新
private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String idStr = req.getParameter("id");
String name = req.getParameter("name");
String address = req.getParameter("address");
String phone = req.getParameter("phone");
String oldName = req.getParameter("oldName");
if(!oldName.equalsIgnoreCase(name)){
long count = customerDAO.getCountWithName(name);
//当名字被占用时
if(count>0) {
req.setAttribute("message", "用户名:" + name + "已被占用,请重新选择");
req.getRequestDispatcher("/updateCustomer.jsp").forward(req, resp);
return;
}
}
Customer customer = new Customer(name, address, phone);
customer.setId(Integer.parseInt(idStr));
customerDAO.update(customer);
resp.sendRedirect("query.do");
}

浙公网安备 33010602011771号