JSTL

1. 概念:JavaServer Pages Tag Library JSP标准标签库

2. 作用:用于简化和替换jsp页面上的java代码

3. 使用步骤:
         1. 导入jstl相关jar包
         2. 引入标签库:taglib指令: <%@ taglib %>
         3. 使用标签

4. 常用的JSTL标签
        1. if:相当于java代码的if语句
                1. 属性:
                            * test 必须属性,接受boolean表达式
                            * 如果表达式为true,则显示if标签体内容,如果为false,则不显示标签体内容
                            * 一般情况下,test属性值会结合el表达式一起使用
                 2. 注意:
                            * c:if标签没有else情况,想要else情况,则可以在定义一个c:if标签

代码:

1 <c:if test="12 == 12">
2     <%="好的"%>
3 </c:if>

         2. choose:相当于java代码的switch语句
                            1. 使用choose标签声明 相当于switch声明
                            2. 使用when标签做判断 相当于case
                            3. 使用otherwise标签做其他情况的声明 相当于default

代码:

 1 <%
 2     request.setAttribute("key",5);
 3 %>
 4 <c:choose>
 5     <c:when test="${requestScope.key >= 10}">
 6         ${"强"}
 7     </c:when>
 8     <c:when test="${requestScope.key >= 3}">
 9         ${"一般"}
10     </c:when>
11     <c:otherwise>
12         ${"菜"}
13     </c:otherwise>
14 </c:choose>

         3. foreach:相当于java代码的for语句

常规代码:

1 <%
2     request.setAttribute("list", new String[]{"111111111", "22222222", "333333333", "444444444"});
3 %>
4 <C:forEach items="${requestScope.list}" var="item">
5     ${item}<br/>
6 </C:forEach>

代码:

student类

1  private Integer id;
2     private String username;
3     private String password;
4     private Integer age;
5     private String phone;

jsp

 1 <%
 2     List<Student> list = new ArrayList<>();
 3     for (int i = 0; i < 10; i++) {
 4         list.add(new Student(i, "aa" + i, "bb" + i, i * 10, "cc" + i));
 5     }
 6     request.setAttribute("student", list);
 7 %>
 8 <table border="1">
 9     <tr>
10         <th>编号</th>
11         <th>用户名</th>
12         <th>密码</th>
13         <th>年龄</th>
14         <th>电话</th>
15         <th>操作</th>
16     </tr>
17     <c:forEach varStatus="status" items="${requestScope.student}" var="stu">
18         <tr>
19             <td>${stu.id}</td>
20             <td>${stu.username}</td>
21             <td>${stu.password}</td>
22             <td>${stu.age}</td>
23             <td>${stu.phone}</td>
24             <td>${status}</td>
25         </tr>
26     </c:forEach>
27 </table>

 

posted @ 2020-03-23 15:08  王余阳  阅读(136)  评论(0)    收藏  举报