<%@ page language="java" import="java.util.*,model.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>JSP JSTL標準標籤庫</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>JSTL核心標籤庫</h3> <% pageContext.setAttribute("abc", null); pageContext.setAttribute("name", "monkey"); pageContext.setAttribute("hello", "<h1>JSTL Core 標籤</h1>"); %> <h3>out標籤</h3> c:out--><c:out value="zhangsan"/><br> <c:out value="${abc }">wangwu</c:out><br> <c:out value="${abc }" default="lisi"/><br> <c:out value="${name }"></c:out> <c:out value="${hello }" escapeXml="false">ljiu</c:out> <hr> <h3>set標籤</h3> <c:set value="java" var="test"/> ${test }<br> <c:set var="test1"> hello jstl </c:set> ${test1 }<br> c:set標籤存儲值到JavaBean屬性中: <jsp:useBean id="stu" class="model.Student"></jsp:useBean> <c:set value="1001" target="${stu }" property="id"></c:set> <c:set value="zhangsan" target="${stu }" property="name"></c:set> ${stu.id }--->${stu.name }<br> <c:set target="${stu }" property="id">1002</c:set> <c:set target="${stu }" property="name">simaguang</c:set> ${stu.id }--->${stu.name }<br> <% pageContext.setAttribute("msg", "這是pageContext的數據"); request.setAttribute("msg", "這是request的數據"); session.setAttribute("msg", "這是session的數據"); application.setAttribute("msg", "這是application的數據"); List<String> list1=new ArrayList(); list1.add("beijing"); list1.add("tianjin"); list1.add("shanghai"); list1.add("xi'an"); request.setAttribute("list1", list1); Student stu1=new Student(1001,"zhangsan"); Student stu2=new Student(1002,"lisi"); List<Student> list2=new ArrayList(); list2.add(stu1); list2.add(stu2); request.setAttribute("list2", list2); String str="welcome to Tianjin"; request.setAttribute("str", str); %> ${pageScope.msg }<br> c:remove標籤:<br> <c:remove var="msg"/> ${requestScope.msg } ${pageScope.msg }<br> c:catch標籤:<br> <c:catch var="info"> <% out.print(10/0); %> </c:catch> ${info }<br> c:choose、c:when、c:otherwise 標籤:<br> <c:choose> <c:when test="${5<4 }">hello</c:when> <c:when test="${3>2 }">world</c:when> <c:otherwise>!</c:otherwise> </c:choose> <br> c:foreach標籤:<br> <c:forEach begin="1" end="10" step="2" var="i"> ${i } </c:forEach> <br> c:forEach迭代集合對象:<br> <c:forEach items="${list1 }" var="address"> ${address } </c:forEach> <br> c:foreach迭代JavaBean對象:<br> <c:forEach items="${list2 }" var="student" varStatus="status"> ${status.first}-->${status.last}-->${status.index }-->${status.count }-->${student.id }---->${student.name }<br> </c:forEach> <br> <table> <tr> <th>ID</th> <th>Name</th> </tr> <c:forEach items="${list2 }" var="student" varStatus="status"> <c:if test="${status.index%2==0 }"> <tr bgcolor="red"> </c:if> <c:if test="${status.index%2!=0 }"> <tr bgcolor="green"> </c:if> <td>${student.id }</td> <td>${student.name }</td> </tr> </c:forEach> </table> <hr> c:forTokens標籤:<br> <c:forTokens items="${str }" delims=" " var="item"> ${item }<br> </c:forTokens> </body> </html>
package model; public class Student { private int id; private String name; public Student(){ } public Student(int id,String name){ this.id=id; this.name=name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }