EL表达式与JSTL标签库(二)

1、JSTL标签库

标签库 作用 URI 前缀
核心 包含Web应用的常见工作,如循环、输入输出等 http://java.sun.com/jsp/jstl/core c
国际化 语言区域、消息、数字和日期格式化 http://java.sun.com/jsp/jstl/fmt fmt

2、Core标签库

  核心标签库分四类:

  • 多用途核心标签。<c:out>、<c:set>、<c:remove>、<c:catch>
  • 条件控制标签。<c:if>、<c:choose>、<c:when>、<c:otherwise>
  • 循环控制标签。<c:forEach>、<c:forTokens>
  • URL相关标签。<c:import>、<c:url>、<c:redirect>、<c:param>

3、<c:if>

属性 描述
test 需要判断条件
var 保存判断结果的变量名,该变量的值为true或false
scope 变量的作用域,默认为page

4、<c:forEach>

属性 描述
begin 开始条件,如果指定items,循环将从begin指定的索引处开始。
end 结束索引,如果指定items,循环将从end指定索引处结束。
step 循环的步长,默认为1
items 遍历的集合对象
var 循环集合元中的素的变量名
varStatus 保存循环状态的变量

5、简单应用

jsp页面对标签库的引用需要提供jstl的jar包,jar包官方网页版说明如下图示,也可以参考帖子https://blog.csdn.net/qq_32115439/article/details/54685786

 1 package com.alphajuns.domain;
 2 
 3 public class Book {
 4 
 5     private Integer id;
 6     private String name;
 7     private String author;
 8     
 9     public Book() {
10         super();
11     }
12     
13     public Book(Integer id, String name, String author) {
14         super();
15         this.id = id;
16         this.name = name;
17         this.author = author;
18     }
19     
20     public Integer getId() {
21         return id;
22     }
23     public void setId(Integer id) {
24         this.id = id;
25     }
26     public String getName() {
27         return name;
28     }
29     public void setName(String name) {
30         this.name = name;
31     }
32     public String getAuthor() {
33         return author;
34     }
35     public void setAuthor(String author) {
36         this.author = author;
37     }
38     
39 }
 1 package com.alphajuns.controller;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.ui.Model;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestMethod;
12 
13 import com.alphajuns.domain.Book;
14 
15 @Controller
16 public class TestController {
17 
18     @RequestMapping(value="/forEachTest", method=RequestMethod.GET)
19     public String registerForm(Model model) {
20         // 页面用于遍历的List集合
21         List<String> strList = new ArrayList<String>();
22         strList.add("JavaEE");
23         strList.add("Mybatis");
24         strList.add("Spring");
25         // 页面用于遍历的List集合
26         List<Book> bookList = new ArrayList<Book>();
27         bookList.add(new Book(1, "Spring实战", "Craig Walls"));
28         bookList.add(new Book(2, "疯狂Java讲义", "李刚"));
29         bookList.add(new Book(3, "通信原理", "樊昌信"));
30         bookList.add(new Book(4, "计算机网络", "谢希仁"));
31         bookList.add(new Book(5, "现代交换原理", "吕瑞宏"));
32         // 定义map
33         Map<Integer, Book> bookMap = new HashMap<Integer, Book>();
34         bookMap.put(1, new Book(1, "C语言程序设计", "谭浩强"));
35         bookMap.put(2, new Book(2, "数据结构", "严蔚敏"));
36         bookMap.put(3, new Book(3, "模拟电子技术", "童诗白"));
37         model.addAttribute("strList", strList);
38         model.addAttribute("bookList", bookList);
39         model.addAttribute("bookMap", bookMap);
40         return "forEachTest";
41     }
42     
43 }
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     <font color="red">普通循环</font>
12     <table width="400" border="1px" cellspacing="0" cellpadding="0">
13         <tr>
14             <td>index</td><td>count</td><td>isFirst</td><td>isLast</td>
15         </tr>
16         <c:forEach begin="1" end="5" step="2" varStatus="vs">
17             <tr>
18                 <td>${vs.index }</td>
19                 <td>${vs.count }</td>
20                 <td>${vs.first }</td>
21                 <td>${vs.last }</td>
22             </tr>
23         </c:forEach>
24     </table>
25     <br>
26     
27     <font color="red">遍历元素为String的List集合</font>
28     <table width="400" border="1px" cellspacing="0" cellpadding="0">
29         <tr>
30             <td>编号</td><td>书名</td><td>作者</td>
31         </tr>
32         <c:forEach items="${requestScope.bookList }" var="book">
33             <tr>
34                 <td>${book.id }</td>
35                 <td>${book.name }</td>
36                 <td>${book.author }</td>
37             </tr>
38         </c:forEach>
39     </table>
40     <br>
41     
42     <font color="red">遍历Map集合</font>
43     <table width="400" border="1px" cellspacing="0" cellpadding="0">
44         <tr>
45             <td>Key</td><td>编号</td><td>书名</td><td>作者</td>
46         </tr>
47         <c:forEach items="${requestScope.bookMap }" var="entry">
48             <tr>
49                 <td>${entry.key }</td>
50                 <td>${entry.value.id }</td>
51                 <td>${entry.value.name }</td>
52                 <td>${entry.value.author }</td>
53             </tr>
54         </c:forEach>
55     </table>
56 </body>
57 </html>

 

原文地址:https://www.cnblogs.com/alphajuns/p/11062050.html