JavaWeb同步学习笔记之三十七、JavaWeb_MVC案例之修改代码实现

JavaWeb_MVC案例之修改代码实现

MVC案例之修改代码实现

  • 1.updatecustomer.jsp
<%@page import="com.xs.mvc_crud.domain.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		Object message = request.getAttribute("message");
	%>

	<font color="red"><%=request.getAttribute("message") == null ? "" : request.getAttribute("message")%></font>
	<%
		
		String id = null;
		String oldName = null;
		String newName = null;
		String newAddress = null;
		String newPhone = null;
		Customer customer = (Customer)request.getAttribute("customer");
		if(customer != null){
			id = customer.getId() + "";
			oldName = customer.getName();
			newName = customer.getName();
			newAddress = customer.getAddress();
			newPhone = customer.getPhone();
			
		}else{
			id = request.getParameter("id");
			oldName = request.getParameter("oldName");
			newName = request.getParameter("oldName");
			newAddress = request.getParameter("newAddress");
			newPhone = request.getParameter("newPhone");	
		}
		
	%>
	<form action="update.do" method="post">
		<!-- 使用隐藏域来保存要修改的Customer对象的id -->
		<input name="id" type="hidden" value="<%=id%>" /> 
		<input name="oldName" type="hidden" value="<%=oldName%>" />
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<td>CustomerName</td>
				<td><input name="newName" type="text"
					value="<%=newName%>" /></td>
			</tr>
			<tr>
				<td>Address</td>
				<td><input name="newAddress" type="text"
					value="<%=newAddress%>" /></td>
			</tr>
			<tr>
				<td>Phone</td>
				<td><input name="newPhone" type="text"
					value="<%=newPhone%>" /></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="Update" /></td>
			</tr>
		</table>
	</form>
</body>
</html>
  • 2.CustomerServlet中edit方法代码:
	/**   
	 * @Title: edit
	 * @Description: TODO
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException 
	 * @return void
	 */
	 private void edit(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException{
		
		String forwardPath = "/error.jsp";
		//1. 获取请求参数 id
		String idStr = request.getParameter("id");
		//2. 调用 CustomerDAO 的 customerDAO.get(id) 获取和 id 对应的 Customer 对象 customer
		try {
			Customer customer = customerDAO.get(Integer.parseInt(idStr));
			if(customer != null){
				forwardPath = "/updatecustomer.jsp";
				//3. 将 customer 放入 request 中
				request.setAttribute("customer", customer);
			}
		} catch (NumberFormatException e) {} 
		//4. 响应 updatecustomer.jsp 页面: 转发.
		request.getRequestDispatcher(forwardPath).forward(request, response);
		
	}
  • 2.CustomerServlet中update方法代码:
	/**
	 * @Title: update
	 * @Description: TODO
	 * @param request
	 * @param response
	 * @return void
	 * @throws IOException
	 * @throws ServletException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//1. 获取表单参数: id, newName, newAddress, newPhone, oldName
		String id = request.getParameter("id");
		String oldName = request.getParameter("oldName");
		String newName = request.getParameter("newName");
		String newAddress = request.getParameter("newAddress");
		String newPhone = request.getParameter("newPhone");

		//2. 检验 name 是否已经被占用:
		//2.1 比较 name 和 oldName 是否相同, 若相同说明 name 可用. 
		//2.1 若不相同, 则调用 CustomerDAO 的 getCountWithName(String name) 获取 name 在数据库中是否存在
		if (!oldName.equals(newName)) {
			String nameStr = customerDAO.getCountWithName(newName).toString();
			int count = Integer.parseInt(nameStr);
			//2.2 若返回值大于 0, 则响应 updatecustomer.jsp 页面: 通过转发的方式来响应 newcustomer.jsp
			if (count > 0) {
				//2.2.1 在 updatecustomer.jsp 页面显示一个错误消息: 用户名 name 已经被占用, 请重新选择!
				//在 request 中放入一个属性 message: 用户名 name 已经被占用, 请重新选择!, 
				//在页面上通过 request.getAttribute("message") 的方式来显示.
				request.setAttribute("message", "对不起,用户名" + newName + "已经被占用,请重新选择!");
				//2.2.2 newcustomer.jsp 的表单值可以回显. 
				//address, phone 显示提交表单的新的值, 而 name 显示 oldName, 而不是新提交的 name.
				//2.2.3 结束方法: return 
				request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response);
				return;
			}
		}
		//3. 若验证通过, 则把表单参数封装为一个 Customer 对象 customer
		Customer customer = new Customer(newName, newAddress, newPhone);
		customer.setId(Integer.parseInt(id));
		customerDAO.update(customer);
		response.sendRedirect("query.do");
	}

猜你喜欢

转载自blog.csdn.net/baidu_38688346/article/details/88374910