JavaWeb17.3【EL&JSTL:JSTL標籤】
阿新 • • 發佈:2021-07-03
1 <%@ page import="java.util.List" %> 2 <%@ page import="java.util.ArrayList" %><%-- 3 Created by IntelliJ IDEA. 4 User: yubaby 5 Date: 2021/7/3 6 Time: 19:33 7 To change this template use File | Settings | File Templates. 8 --%> 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 10 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 11 12 <html> 13 <head> 14 <title>JSTL常用標籤:if</title> 15 </head> 16 <body> 17 18 <%-- 19 c:if標籤 20 1. 屬性: 21 * test 必須屬性,接受boolean表示式22 * 如果表示式為true,則顯示if標籤體內容;如果為false,則不顯示標籤體內容 23 * 一般情況下,test屬性值會結合el表示式一起使用 24 2. 注意: 25 * c:if標籤沒有else情況,想要else情況,則可以在定義一個c:if標籤 26 --%> 27 <c:if test="true"> 28 <h1>真的</h1> 29 </c:if> 30<c:if test="flase"> 31 <h1>假的</h1> 32 </c:if> 33 34 35 <% 36 //判斷request域中的一個list集合是否為空,若不為null則顯示遍歷該集合 37 38 List list = new ArrayList(); 39 list.add("aaa"); 40 list.add("bbb"); 41 request.setAttribute("list", list); 42 43 request.setAttribute("num", 3); 44 %> 45 46 <c:if test="${not empty list}"> 47 ${requestScope.list} 48 </c:if> 49 <br> 50 51 <c:if test="${num % 2 != 0}"> 52 ${num}是奇數 53 </c:if> 54 <c:if test="${num % 2 == 0}"> 55 ${num}是偶數 56 </c:if> 57 58 </body> 59 </html>
1 <%-- 2 Created by IntelliJ IDEA. 3 User: yubaby 4 Date: 2021/7/3 5 Time: 20:12 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 10 11 <html> 12 <head> 13 <title>JSTL常用標籤:choose</title> 14 </head> 15 <body> 16 17 <%-- 18 完成數字編號對應星期幾案例 19 1.域中儲存一數字 20 2.使用choose標籤取出數字 相當於switch宣告 21 3.使用when標籤做數字判斷 相當於case 22 4.otherwise標籤做其他情況的宣告 相當於default 23 --%> 24 25 <% 26 request.setAttribute("num", 3); 27 %> 28 <c:choose> 29 <c:when test="${num == 1}">星期一</c:when> 30 <c:when test="${num == 2}">星期二</c:when> 31 <c:when test="${num == 3}">星期三</c:when> 32 <c:when test="${num == 4}">星期四</c:when> 33 <c:when test="${num == 5}">星期五</c:when> 34 <c:when test="${num == 6}">星期六</c:when> 35 <c:when test="${num == 7}">星期天</c:when> 36 <c:otherwise>輸入有誤</c:otherwise> 37 </c:choose> 38 39 </body> 40 </html>
1 <%@ page import="java.util.List" %> 2 <%@ page import="java.util.ArrayList" %><%-- 3 Created by IntelliJ IDEA. 4 User: yubaby 5 Date: 2021/7/3 6 Time: 20:19 7 To change this template use File | Settings | File Templates. 8 --%> 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 10 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 11 <html> 12 <head> 13 <title>JSTL常用標籤:foreach</title> 14 </head> 15 <body> 16 17 <%-- 18 19 foreach:相當於java程式碼的for語句 20 1. 完成重複的操作 21 for(int i = 0; i < 10; i++){} 22 * 屬性: 23 begin:開始值 24 end:結束值 25 var:臨時變數 26 step:步長 27 varStatus:迴圈狀態物件 28 index:容器中元素的索引,從0開始 29 count:迴圈次數,從1開始 30 2. 遍歷容器 31 List<User> list; 32 for(User user: list){} 33 * 屬性: 34 items:容器物件 35 var:容器中元素的臨時變數 36 varStatus:迴圈狀態物件 37 index:容器中元素的索引,從0開始 38 count:迴圈次數,從1開始 39 --%> 40 41 <c:forEach begin="1" end="10" var="i" step="1"> <%--屬性順序嚴格--%> 42 ${i}<br> 43 </c:forEach> 44 <hr> 45 46 <c:forEach begin="1" end="10" var="i" step="2"> 47 ${i}<br> 48 </c:forEach> 49 <hr> 50 51 <c:forEach begin="1" end="10" var="i" step="2" varStatus="s"> 52 ${i} ${s.index} ${s.count}<br> 53 </c:forEach> 54 <hr> 55 56 57 <% 58 List list = new ArrayList(); 59 list.add("aaa"); 60 list.add("bbb"); 61 list.add("ccc"); 62 request.setAttribute("list", list); 63 %> 64 <c:forEach items="${list}" var="str" varStatus="s"> 65 ${str} ${s.index} ${s.count}<br> 66 </c:forEach> 67 68 </body> 69 </html>
1 package com.haifei.domain; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class User { 7 private String name; 8 private int age; 9 private Date birthday; 10 11 /** 12 * 邏輯檢視 13 * (並沒有專門對應的成員屬性birthdayStr,而僅僅是處理一下birthday方便頁面展示) 14 * @return 15 */ 16 public String getBirthdayStr(){ 17 //格式化日期物件 18 if (birthday != null){ 19 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 20 /*String birthdayStr = sdf.format(birthday); 21 return birthdayStr;*/ 22 return sdf.format(birthday); 23 }else{ 24 return ""; 25 } 26 } 27 28 public User() { 29 } 30 31 public User(String name, int age, Date birthday) { 32 this.name = name; 33 this.age = age; 34 this.birthday = birthday; 35 } 36 37 public String getName() { 38 return name; 39 } 40 41 public int getAge() { 42 return age; 43 } 44 45 public Date getBirthday() { 46 return birthday; 47 } 48 49 public void setName(String name) { 50 this.name = name; 51 } 52 53 public void setAge(int age) { 54 this.age = age; 55 } 56 57 public void setBirthday(Date birthday) { 58 this.birthday = birthday; 59 } 60 }
1 <%@ page import="java.util.List" %> 2 <%@ page import="java.util.ArrayList" %> 3 <%@ page import="com.haifei.domain.User" %> 4 <%@ page import="java.util.Date" %><%-- 5 Created by IntelliJ IDEA. 6 User: yubaby 7 Date: 2021/7/3 8 Time: 20:36 9 To change this template use File | Settings | File Templates. 10 --%> 11 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 12 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 13 <html> 14 <head> 15 <title>el+jstl案例</title> 16 </head> 17 <body> 18 <% 19 List list = new ArrayList(); 20 list.add(new User("zhangsan", 23, new Date())); 21 list.add(new User("lisi", 22, new Date())); 22 list.add(new User("wangwu", 24, new Date())); 23 request.setAttribute("list", list); 24 %> 25 26 <table border="1" width="500" align="center"> 27 <tr> 28 <th>編號</th> 29 <th>姓名</th> 30 <th>年齡</th> 31 <th>生日</th> 32 </tr> 33 <%--資料行--%> 34 <c:forEach items="${list}" var="user" varStatus="s"> 35 <c:if test="${s.count % 2 == 0}"> 36 <tr bgcolor="red"> 37 <td>${s.count}</td> 38 <td>${user.name}</td> 39 <td>${user.age}</td> 40 <td>${user.birthdayStr}</td> 41 </tr> 42 </c:if> 43 <c:if test="${s.count % 2 != 0}"> 44 <tr bgcolor="green"> 45 <td>${s.count}</td> 46 <td>${user.name}</td> 47 <td>${user.age}</td> 48 <td>${user.birthdayStr}</td> 49 </tr> 50 </c:if> 51 </c:forEach> 52 </table> 53 </body> 54 </html>