replace JSP scriplet code with JSTL Tags
--
JSP scriplet code:
<%@ page import="java.util.*, com.luv2code.web.jdbc.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Student Tracker App</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
</head>
<%
// get student from the request object (sent by servlet)
List<Student> theStudents =
(List<Student>) request.getAttribute("STUDENT_LIST");
%>
<body>
<div id="wrapper">
<div id="header">
<h2>FooBar University</h2>
</div>
</div>
<div id="container">
<div id="content">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<% for (Student tempStudent: theStudents) { %>
<tr>
<td><%= tempStudent.getFirstName() %></td>
<td><%= tempStudent.getLastName() %></td>
<td><%= tempStudent.getEmail() %></td>
</tr>
<% } %>
</table>
</div>
</div>
</body>
</html>
--
replace it with JSTL Tags
- 首先检查,library里面有jsp.jstl 和jsp.jstl-api 这两个jar文件,如图:
- 检查完之后修改list-students.jsp 代码如下:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>Student Tracker App</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>FooBar University</h2>
</div>
</div>
<div id="container">
<div id="content">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<c:forEach var="tempStudent" items="${STUDENT_LIST}">
<tr>
<td> ${tempStudent.firstName}</td>
<td> ${tempStudent.lastName}</td>
<td> ${tempStudent.email}</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
网友评论