JSTL(JSP标准标签库)
JSP标准标签库(JavaServer Pages Tag Library, JSTL)是一个定制JSP标签库的集合,封装了JSP应用的通用核心功能。用来解决像遍历Map或集合、条件测试、XML处理,甚至数据库访问和数据操作等常见的问题。
使用JSTL前的准备
JSTL的相关jar包可以从Tomcat的官网下载
使用JSTL前需要把下面的JAR包放到 "/WEB-INF/lib" 目录下
- taglibs-standard-spec-1.2.5.jar
- taglibs-standard-impl-1.2.5.jar
- taglibs-standard-jstlel-1.2.5.jar
- xalan-2.7.1.jar
- serializer-2.7.1.jar
其 maven 坐标为
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-spec --> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>1.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-jstlel --> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-jstlel</artifactId> <version>1.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/xalan/xalan --> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.2</version> </dependency> <!-- https://mvnrepository.com/artifact/xalan/serializer --> <dependency> <groupId>xalan</groupId> <artifactId>serializer</artifactId> <version>2.7.2</version> </dependency>
xalan-2.7.1.jar和serializer-2.7.1.jar可从下面网站很方便下载得到,
进入Jar File Dowlaod后,根据字母排序找到想要的Jar包,下载即可。
根据JSTL标签所提供的功能,可以将其方位5个大类
在JSP页面中使用JSTL,必须通过下面的格式使用 taglib 指令
<%@ taglib uri="uri" prefix="prefix" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!-- 核心标签 --> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <!-- 格式化标签 --> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <!-- SQL标签 --> <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> <!-- XML标签 --> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <!-- JSTL函数 -->
JSTL中具体标签的详细说明可参见以下网站
截图概述
核心标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
使用 <c:if> 判断属性的值,下面的代码为判断 requestScope 范围中的属性 taxid 的值是我否为 “taxid”
requestScope 是 EI 内置对象
test 只接受 boolean 值
<c:if test="${requestScope.taxid == 'taxid' }" > <th>taxId</th> </c:if> <c:if test="${requestScope.taxid != null }" > <th>taxId</th> </c:if>
格式化标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
SQL标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
XML标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
JSTL函数 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
使用JSTL的例子
Servlet作为Controller
package app05a.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import app05a.model.Book; @WebServlet(name = "BookServlet", urlPatterns = { "/books" }) public class BookServlet extends HttpServlet { private static final long serialVersionUID = 1L; public BookServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Book> books = new ArrayList<Book>(); Book book1 = new Book("978-0980839616", "Java 7: A Beginner's Tutorial", 45.00); Book book2 = new Book("978-0980331608", "Structs 2 Design and programming: A Tutorial", 49.95); Book book3 = new Book("978-0975212820", "Dimensional Data Warehousing with MySQL: A Tutorial", 39.95); books.add(book1); books.add(book2); books.add(book3); request.setAttribute("books", books); RequestDispatcher rd = request.getRequestDispatcher("/books.jsp"); rd.forward(request, response); } }
JavaBean作为Model
package app05a.model; public class Book { private String isbn; private String title; private double price; public Book() {} public Book(String isbn, String title, double price) { this.isbn = isbn; this.title = title; this.price = price; } public String getIsbn() { return isbn; } public void setisbn(String isbn) { this.isbn = isbn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
package app05a.model; public class Book { private String isbn; private String title; private double price; public Book() {} public Book(String isbn, String title, double price) { this.isbn = isbn; this.title = title; this.price = price; } public String getIsbn() { return isbn; } public void setisbn(String isbn) { this.isbn = isbn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
JSP页面作为view
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Book List</title> <style> table, tr, td { border: 1px solid brown; } </style> </head> <body> Books in Simple table; <table> <tr> <td>ISBIN</td> <td>Title</td> </tr> <c:forEach items="${requestScope.books }" var="book"> <tr> <td>${book.isbn }</td> <td>${book.title }</td> </tr> </c:forEach> </table> <br /> Books in Styled Table <table> <tr style="background:#c0c0c0"> <td>ISBN</td> <td>Title</td> </tr> <c:forEach items="${requestScope.books }" var="book" varStatus="status"> <c:if test="${status.count % 2 == 0 }"> <!-- 偶数行 --> <tr style="background:#ffff00"> </c:if> <c:if test="${status.count %2 != 0 }"> <!-- 奇数行 --> <tr style="background:#00ffff"> </c:if> <td>${book.isbn }</td> <td>${book.title }</td> </c:forEach> </table> <br /> ISBNs only: <c:forEach items="${requestScope.books }" var="book" varStatus="status"> ${book.isbn } <c:if test="${!status.last }">,</c:if> <!-- 表明当前这轮迭代是否为最后一次迭代的标志 --> </c:forEach> </body> </html>
效果
使用<c:forEach>遍历Map
Servlet作为Controller
package app05a.servlet; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "CitiesServlet", urlPatterns = { "/cities" }) public class CitiesServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CitiesServlet() { super(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String, String> capitals = new HashMap<String, String>(); capitals.put("Indonesia", "Jakarta"); capitals.put("Thailand", "Bangkok"); capitals.put("Malaysia", "Kuala Lumpur"); request.setAttribute("capitals", capitals); Map<String, String[]> bigCities = new HashMap<>(); bigCities.put("Australia", new String[] {"Sydney","Melbourne","Perth"}); bigCities.put("New Zealand", new String[] {"Auchland","Christchurch","Wellington"}); bigCities.put("Indonesia", new String[] {"Jakarta","Surabaya","Medan"}); request.setAttribute("bigCities", bigCities); RequestDispatcher rd = request.getRequestDispatcher("/cities.jsp"); rd.forward(request, response); } }
JSP页面作为view
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Cities</title> <style> table, tr, td { border: 1px solid #aaee77; padding:3px; } </style> </head> <body> Capitals <br /> <table> <tr style="background:#448755; color:white; font-weight:bold;"> <td>Country</td> <td>Capital</td> </tr> <c:forEach items="${requestScope.capitals }" var="mapItem"> <!-- 遍历Map --> <tr> <td>${mapItem.key }</td> <!-- 获取key --> <td>${mapItem.value }</td> <!-- 获取value --> </tr> </c:forEach> </table> <br /> Big Cities <br /> <table> <tr style="background:#448755; color:white; font-weight:bold;"> <td>Country</td> <td>BigCities</td> </tr> <c:forEach items="${requestScope.bigCities }" var="mapItem"> <tr> <td>${mapItem.key }</td> <td> <c:forEach items="${mapItem.value }" var="city" varStatus="status"> <!-- Map的value是数组,也需要遍历 --> ${city }<c:if test="${!status.last }">,</c:if> </c:forEach> </td> </tr> </c:forEach> </table> </body> </html>
效果