美文网首页
replace JSP scriplet code

replace JSP scriplet code

作者: Zihowe | 来源:发表于2017-07-26 14:17 被阅读15次

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文件,如图:
image.png
  • 检查完之后修改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>

相关文章

网友评论

      本文标题:replace JSP scriplet code

      本文链接:https://www.haomeiwen.com/subject/fcankxtx.html