JavaWeb同步学习笔记之六十八、JavaWeb_JSTL_改写MVC案例

JavaWeb_JSTL_改写MVC案例

JSTL_改写MVC案例

  • 1.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<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>
			<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>
	<hr>
	<br>
	<br>
	<c:if test="${!empty requestScope.customers }">
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<th>Id</th>
				<th>CustomerName</th>
				<th>Address</th>
				<th>Phone</th>
				<th>UPDATE</th>
				<th>DELETE</th>
			</tr>
			<c:forEach items="${requestScope.customers }" var="customer">
				<tr>
					<td>${customer.id }</td>
					<td>${customer.name }</td>
					<td>${customer.address }</td>
					<td>${customer.phone }</td>
					<td>
						<c:url value="/edit.do" var="editurl">
							<c:param name="id" value="${customer.id }"></c:param>
						</c:url>
						<a href="${editurl }">Edit</a>
					</td>
					<td>
						<c:url value="/delete.do" var="deleteurl">
							<c:param name="id" value="${customer.id }"></c:param>
						</c:url>
						<a class="delete" href="${deleteurl }">Delete</a>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
</body>
</html>
  • 2.newcustomer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>

<body>

	<c:if test="${requestScope.message != null }">
		<br>
		<font color="red">${requestScope.message }</font>
		<br>
	</c:if>

	<form action="add.do" method="post">
		<table>
			<tr>
				<td>name:</td>
				<td><input id="name" type="text" name="name" value="${param.name }" /></td>
			</tr>
			<tr>
				<td>address:</td>
				<td><input id="address" type="text" name="address" value="${param.address }" /></td>
			</tr>
			<tr>
				<td>phone:</td>
				<td><input id="phone" type="text" name="phone" value="${param.phone }" /></td>
			</tr>
			<tr>
				<td colspan="2"><input id="add" type="submit" value="Add" /></td>
			</tr>
		</table>
	</form>

</body>
</html>
  • 3.updatecustomer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<c:if test="${requestScope.message != null }">
		<br>
		<font color="red">${requestScope.message }</font>
		<br>
	</c:if>
	
	<c:set var="id" value="${customer != null ? customer.id : param.id }"></c:set>
	<c:set var="oldName" value="${customer != null ? customer.name : param.oldName }"></c:set>
	<c:set var="newName" value="${customer != null ? customer.name : param.oldName }"></c:set>
	<c:set var="newAddress" value="${customer != null ? customer.address : param.newAddress }"></c:set>
	<c:set var="newPhone" value="${customer != null ? customer.phone : param.newPhone }"></c:set>
	
	<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值