实现 data.jsp 页面
在这一步中,你将学习如何实现 data.jsp
页面,该页面将使用 EL 表达式显示从数据库中检索到的用户数据。
- 打开位于
QueryDisplayElProject/src/main/webapp
中的 data.jsp
文件。
- 向该文件添加以下代码:
<%@ page import="org.labex.entity.User" %> <%@ page
contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户信息</title>
<style>
th,
td {
border: 1px solid black;
padding: 8px;
}
</style>
</head>
<body>
<c:if test="${not empty requestScope.user}">
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话号码</th>
<th>邮箱</th>
<th>地址</th>
</tr>
</thead>
<tbody>
<tr>
<td>${user.userId}</td>
<td>${requestScope.user.userName}</td>
<td>${requestScope.user.userGender == 1? '男' : '女'}</td>
<td>${requestScope.user.userAge}</td>
<td>${requestScope.user.userPhone}</td>
<td>${requestScope.user.userEmail}</td>
<td>${requestScope.user.userAddr}</td>
</tr>
</tbody>
</table>
</c:if>
<c:if test="${empty requestScope.user}">
<p>未找到信息。</p>
</c:if>
</body>
</html>
这段代码创建了一个 HTML 表格来显示用户数据。它使用 EL 表达式来访问从 Servlet
检索并存储在 requestScope
中的 User
对象的属性。如果未找到 User
对象,它将显示一条消息,表明未找到信息。
<c:if>
标签用于根据 requestScope
中是否存在 User
对象有条件地显示表格或“未找到信息”消息。
- 使用以下命令启动 Tomcat 服务器:
cd ~/project/QueryDisplayElProject/
mvn clean tomcat7:run
- 打开一个网页浏览器并转到
http://localhost:8080
。你将看到一个输入框和一个按钮,在输入框中输入数字 id,例如:7
,然后点击按钮跳转到 data.jsp
页面并显示 id 为 7
的用户信息。
结果如下: