JavaWeb_MVC案例之新增Customer
MVC案例之新增Customer
- 1.添加的流程。
1)Add New Customer 超链接连接到 newcustomer.jsp
2)新建 newcustomer.jsp:
3)在 CustomerServlet 的 add 方法中:参见注释
- 2.具体的实现代码:
index.jsp中form表单的代码:
<form action="query.do" method="post">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td><input type="submit" value="Query" /></td>
<td><a href="newcustomer.jsp">Add New Customer</a></td>
</tr>
</table>
</form>
newcustomer.jsp的代码:
<%@ 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>
<font color="red"><%=request.getAttribute("message") == null ? "" : request.getAttribute("message")%></font>
<form action="add.do" method="post">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>Name:</td>
<td><input id="name" type="text" name="name"
value="<%=request.getParameter("name") == null ? "" : request.getParameter("name")%>" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input id="address" type="text" name="address"
value="<%=request.getParameter("address") == null ? "" : request.getParameter("address")%>" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input id="phone" type="text" name="phone"
value="<%=request.getParameter("phone") == null ? "" : request.getParameter("phone")%>" /></td>
</tr>
<tr>
<td colspan="2"><input id="add" type="submit" value="Add" /></td>
</tr>
</table>
</form>
</body>
</html>
CustomerServlet中add方法的代码:
/**
* @Title: add
* @Description: TODO
* @param request
* @param response
* @return void
* @throws IOException
* @throws ServletException
*/
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 获取表单参数:name, address, phone
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
//2 检验name是否已经被占用:
//2.1 调用CustomerDAO的getCountWithName(String name)获取name在数据库中是否存在。
String nameStr = customerDAO.getCountWithName(name).toString();
int count = Integer.parseInt(nameStr);
//2.2 若返回值大于0,则响应newcustomer.jsp页面:
if (count > 0) {
//2.2.1 要求在newcustomer.jsp页面显示一个错误消息:用户名name已经被占用,请重新选择!
//在request中放入一个属性message:用户名name已经被占用,请重新选择!
//在页面上通过request.getAttribute("message")的方式来显示。
request.setAttribute("message", "用户名" + name + "已经被占用,请重新选择!");
//2.2.2 newcustomer.jsp的表单值可以回显。
//通过value="<%=request.getParameter("name") == null ? "" : request.getParameter("name")%>" 来回显
//2.2.3结束方法:return
request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
return;
}
//3. 若验证通过,则把表单参数封装为一个Customer对象customer
Customer customer = new Customer(name, address, phone);
//4. 调用CustomerDAO的save(Customer customer)执行保存操作
customerDAO.save(customer);
//5. 使用重定向可以避免出现表单的重复提交问题。
response.sendRedirect("success.jsp");
}