前台展示的Jsp页面如下:
Index.jsp:
%@page import="java.util.List"%
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding="UTF-8"%>
%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %
Insert title here
<form action="query.action" 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="查询全部"/></td>
<td><a href="${baseUrl}/toAddCustomer.action">Create New Customer</a></td>
</tr>
</table>
</form>
<br><br>
<c:if test="${!empty requestScope.customers }">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>Name:</td>
<td>Address:</td>
<td>Phone:</td>
<td>Update</td>
<td>Delete</td>
</tr>
<c:forEach items="${requestScope.customers }" var="cust">
<tr>
<td>${cust.name }</td>
<td>${cust.address }</td>
<td>${cust.phone }</td>
<td><a href="${baseUrl}/getCustomer.action?id=${cust.id}">UPDATE</a></td>
<td><a href="${baseUrl}/delete.action?id=${cust.id}">DELETE</a></td>
</tr>
</c:forEach>
</table>
</c:if>
<!--
已经执行查询, 但返回的集合中没有任何元素时, 显示: 没有符合条件的员工信息.
-->
<c:if test="${requestScope.customers != null }">
<c:if test="${empty requestScope.customers }">
没有符合条件的员工信息.
</c:if>
</c:if>
addCustomer.jsp:
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding="UTF-8"%>
%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %
Insert title here
<br><br>
<form:form action="saveCustomer.action" method="post"
modelAttribute="customer">
<table>
<tr>
<td>Name: </td>
<td>
<form:input path="name"/>
</td>
</tr>
<tr>
<td>Address: </td>
<td>
<form:input path="address"/>
</td>
</tr>
<tr>
<td>Phone: </td>
<td>
<form:input path="phone"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
updateCustomer.jsp:
%@page import="java.util.List"%
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding="UTF-8"%>
%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %
%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %
Insert title here
<c:set value="${requestScope.customer}" var="cust"></c:set> <form:form action="update.action" method="POST" modelAttribute="customer"> <input type="hidden" name="id" value="${cust.id}"> Name: <input type="text" name="name" value="${cust.name }" /><br> Address: <input type="text" name="address" value="${cust.address }"/><br> Phone:<input type="text" name="phone" value="${cust.phone }"/><br> <input type="submit" value="Update"/> </form:form>
数据库表结构:
Create Table
CREATE TABLE customers
(
ID
int(11) NOT NULL AUTO_INCREMENT,
Name
varchar(255) DEFAULT NULL,
ADDRESS
varchar(255) DEFAULT NULL,
PHONE
varchar(255) DEFAULT NULL,
PRIMARY KEY (ID
)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gb2312