JavaWeb17.3【EL&JSTL:JSTL标签】

 

 

 

 

 

 

 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>

 

posted @ 2021-07-03 20:51  yub4by  阅读(37)  评论(0编辑  收藏  举报