编程语言
首页 > 编程语言> > JavaWeb同步学习笔记之三十三、JavaWeb_MVC案例之删除操作

JavaWeb同步学习笔记之三十三、JavaWeb_MVC案例之删除操作

作者:互联网

JavaWeb_MVC案例之删除操作

MVC案例之删除操作

<%@page import="com.xs.mvc_crud.domain.Customer"%>
<%@page import="java.util.List"%>
<%@ 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>
<!-------------------导入jquery-1.7.2.js------------------->
<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
<!-------------------删除提示信息------------------->
<script type="text/javascript">
	$(function () {
		$(".delete").click(function name() {
			var content = $(this).parent().parent().find("td:eq(1)").text();
			var flag = confirm("确定要删除" + content + "的信息吗?");
			return flag;
		});
	});
</script>
</head>
<body>

	<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="">Add New Customer</a></td>
			</tr>
		</table>
	</form>
	
	<br>
	<br>
	<hr>
	<br>
	<br>
	
	<%
		List<Customer> customers = (List<Customer>)request.getAttribute("customer");
		if(customers != null && customers.size() > 0) {
	%>
	<table border="1" cellpadding="10" cellspacing="0">
		<tr>
			<th>Name</th>
			<th>Address</th>
			<th>Phone</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
		<%
			for(Customer customer: customers) {
		%>
		<tr>
			<td><%= customer.getName() %></td>
			<td><%= customer.getAddress() %></td>
			<td><%= customer.getPhone() %></td>
			<td><a href="">Edit</a></td>
			<td><a class="delete" href="delete.do?id=<%= customer.getId()%>">Delete</a></td>
		</tr>
		<%
			}
		%>
	</table>
	<%
		}
	%>

</body>
</html>

CustomerServlet中delete()方法的具体实现:

/**   
	 * @Title: delete
	 * @Description: TODO
	 * @param request
	 * @param response 
	 * @return void
	 * @throws IOException 
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String idStr = request.getParameter("id");
		int id = 0;

		// try-catch的作用:防止idStr不能转为int类型
		// 若不能转则id=0,无法进行任何的删除操作。
		try {
			id = Integer.parseInt(idStr);
			customerDAO.delete(id);
		} catch (NumberFormatException e) {
			//delete.do?id=xxx,如果xxx参数出现错误,直接重定向到query.do。
			response.sendRedirect("query.do");
		}	
	}

标签:do,三十三,JavaWeb,删除,request,MVC,id,delete
来源: https://blog.csdn.net/baidu_38688346/article/details/88368426