JSTL代表JSP标准标签库。 JSTL是提供标签以控制JSP页面行为的标准标签库。 JSTL标签可用于迭代和控制语句,国际化,SQL等。 我们将在本JSTL教程中详细讨论JSTL标签。 之前,我们看到了如何使用JSP EL和JSP Action Tags编写类似HTML的JSP代码,但它们的功能非常有限。 例如,我们无法使用EL或操作元素循环遍历集合,也无法转义HTML标签以在客户端显示它们为文本。 这就是JSTL标签派上用场的地方,因为我们可以使用JSTL标签做更多的事情。
JSTL
JSTL是Java EE API的一部分,并包含在大多数Servlet容器中。但是要在我们的JSP页面中使用JSTL,我们需要为您的Servlet容器下载JSTL jars。大多数情况下,您可以在服务器下载的示例项目中找到它们,并可以使用它们。您需要将这些库包含在您的Web应用程序项目的WEB-INF/lib目录中。
JSTL jars
JSTL jars是特定于容器的,例如,在Tomcat中,我们需要在项目构建路径中包含jstl.jar
和standard.jar
jar文件。如果它们不在容器的lib目录中,则应将它们包含到您的应用程序中。如果您有maven项目,则应在pom.xml
文件中添加以下依赖项,否则您将在JSP页面中收到以下错误 – eclipse Can not find the tag library descriptor for "https://java.sun.com/jsp/jstl/ core"
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
JSTL Tags
根据JSTL功能,它们被分类为五种类型。
-
JSTL 核心标签: JSTL 核心标签提供了对迭代、条件逻辑、异常捕获、URL、转发或重定向响应等的支持。要使用 JSTL 核心标签,我们应该在 JSP 页面中包含如下内容。
<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
在本文中,我们将介绍重要的 JSTL 核心标签。
-
JSTL 格式化和本地化标签: JSTL 格式化标签用于对数字、日期以及通过语言环境和资源包的国际化支持进行格式化。我们可以使用以下语法在 JSP 中包含这些 JSTL 标签:
<%@ taglib uri="https://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
-
JSTL SQL 标签: JSTL SQL 标签提供了与关系型数据库(如 Oracle、MySql 等)交互的支持。使用 JSTL SQL 标签,我们可以运行数据库查询,以下是在 JSP 中包含这些 JSTL 标签的语法:
<%@ taglib uri="https://java.sun.com/jsp/jstl/sql" prefix="sql" %>
-
JSTL XML 标签:JSTL XML 标签用于处理 XML 文档,例如解析 XML、转换 XML 数据和评估 XPath 表达式。在 JSP 页面中包含 JSTL XML 标签的语法如下:
<%@ taglib uri="https://java.sun.com/jsp/jstl/xml" prefix="x" %>
-
JSTL 函数标签:JSTL 标签提供了许多函数,我们可以使用这些函数执行常见操作,其中大多数是用于字符串操作,如字符串连接、拆分字符串等。在 JSP 页面中包含 JSTL 函数的语法如下:
<%@ taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn" %>
注意,所有 JSTL 标准标签的 URI 都以 https://java.sun.com/jsp/jstl/
开头,我们可以使用任何前缀,但最好使用上面定义的前缀,因为每个人都使用它们,这样不会造成混淆。
JSTL核心标签
JSTL核心标签列在下表中。
JSTL Core Tag | Description |
---|---|
<c:out> | To write something in JSP page, we can use EL also with this tag |
<c:import> | Same as jsp:include or include directive |
<c:redirect> | redirect request to another resource |
<c:set> | To set the variable value in given scope. |
<c:remove> | To remove the variable from given scope |
<c:catch> | To catch the exception and wrap it into an object. |
<c:if> | Simple conditional logic, used with EL and we can use it to process the exception from <c:catch> |
<c:choose> | Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <c:when> and <c:otherwise> |
<c:when> | Subtag of <c:choose> that includes its body if its condition evalutes to ‘true’. |
<c:otherwise> | Subtag of <c:choose> that includes its body if its condition evalutes to ‘false’. |
<c:forEach> | for iteration over a collection |
<c:forTokens> | for iteration over tokens separated by a delimiter. |
<c:param> | used with <c:import> to pass parameters |
<c:url> | to create a URL with optional query string parameters |
JSTL教程
让我们在一个简单的Web应用程序中查看一些JSTL核心标签的用法。我们的项目将包括一个Java Bean,并创建一个对象列表,设置一些属性,这些属性将在JSP中使用。JSP页面将展示如何迭代集合,使用EL进行条件逻辑以及一些其他常见用法。
JSTL教程 – Java Bean类
package com.journaldev.model;
public class Employee {
private int id;
private String name;
private String role;
public Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
JSTL教程 – Servlet类
package com.journaldev.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 com.journaldev.model.Employee;
@WebServlet("/HomeServlet")
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setId(1); emp1.setName("Pankaj");emp1.setRole("Developer");
Employee emp2 = new Employee();
emp2.setId(2); emp2.setName("Meghna");emp2.setRole("Manager");
empList.add(emp1);empList.add(emp2);
request.setAttribute("empList", empList);
request.setAttribute("htmlTagData", "<br/> creates a new line.");
request.setAttribute("url", "https://www.journaldev.com");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/home.jsp");
rd.forward(request, response);
}
}
JSTL教程 – JSP页面
home.jsp
代码:
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Home Page</title>
<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<style>
table,th,td
{
border:1px solid black;
}
</style>
</head>
<body>
<%-- Using JSTL forEach and out to loop a list and display items in table --%>
<table>
<tbody>
<tr><th>ID</th><th>Name</th><th>Role</th></tr>
<c:forEach items="${requestScope.empList}" var="emp">
<tr><td><c:out value="${emp.id}"></c:out></td>
<td><c:out value="${emp.name}"></c:out></td>
<td><c:out value="${emp.role}"></c:out></td></tr>
</c:forEach>
</tbody>
</table>
<br><br>
<%-- simple c:if and c:out example with HTML escaping --%>
<c:if test="${requestScope.htmlTagData ne null }">
<c:out value="${requestScope.htmlTagData}" escapeXml="true"></c:out>
</c:if>
<br><br>
<%-- c:set example to set variable value --%>
<c:set var="id" value="5" scope="request"></c:set>
<c:out value="${requestScope.id }" ></c:out>
<br><br>
<%-- c:catch example --%>
<c:catch var ="exception">
<% int x = 5/0;%>
</c:catch>
<c:if test = "${exception ne null}">
<p>Exception is : ${exception} <br>
Exception Message: ${exception.message}</p>
</c:if>
<br><br>
<%-- c:url example --%>
<a href="<c:url value="${requestScope.url }"></c:url>">JournalDev</a>
</body>
</html>
现在当我们使用URL https://localhost:8080/JSTLExample/HomeServlet
运行应用程序时,我们将获得如下图所示的响应。 在上述JSTL示例中,我们使用
c:catch
来捕获JSP服务方法中的异常,这与使用错误页面配置的JSP异常处理不同。这就是关于JSTL标签的快速总结以及JSTL核心标签使用示例。参考:JSTL Wikipedia页面
Source:
https://www.digitalocean.com/community/tutorials/jstl-tutorial-jstl-tags-example