jstl-api jstl_JSTL教程,JSTL标签示例

JSTL是JSP标准标签库,提供控制JSP页面行为的标签,如迭代、控制语句、国际化和SQL操作。在本文中,我们将探讨JSTL的核心标签,包括其在迭代、条件逻辑、异常处理等方面的应用,并通过一个简单的Web应用程序展示如何使用JSTL与Java Bean、Servlet和JSP页面配合工作。
摘要由CSDN通过智能技术生成

jstl-api jstl

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc. We will look into JSTL Tags in detail in this JSTL tutorial.

JSTL代表JSP标准标记库 。 JSTL是标准的标记库,它提供用于控制JSP页面行为的标记。 JSTL标签可用于迭代和控制语句,国际化,SQL等。我们将在此JSTL教程中详细研究JSTL标签。

Earlier we saw how we can use JSP EL and JSP Action Tags to write JSP code like HTML but their functionality is very limited. For example, we can’t loop through a collection using EL or action elements and we can’t escape HTML tags to show them like text in client side. This is where JSTL tags comes handy because we can do much more from JSTL tags.

前面我们看到了如何使用JSP ELJSP动作标签来编写HTML之类的JSP代码,但是它们的功能非常有限。 例如,我们不能使用EL或action元素遍历一个集合,也不能逃脱HTML标签以在客户端将其显示为文本。 这是JSTL标签派上用场的地方,因为我们可以从JSTL标签做更多的事情。

JSTL (JSTL)

JSTL is part of the Java EE API and included in most servlet containers. But to use JSTL in our JSP pages, we need to download the JSTL jars for your servlet container. Most of the times, you can find them in the example projects of server download and you can use them. You need to include these libraries in your web application project WEB-INF/lib directory.

JSTL是Java EE API的一部分,并包含在大多数Servlet容器中。 但是要在我们的JSP页面中使用JSTL,我们需要为您的servlet容器下载JSTL jar。 大多数时候,您可以在服务器下载的示例项目中找到它们,并可以使用它们。 您需要将这些库包含在Web应用程序项目WEB-INF / lib目录中。

JSTL罐子 (JSTL jars)

JSTL jars are container specific, for example in Tomcat, we need to include jstl.jar and standard.jar jar files in project build path. If they are not present in the container lib directory, you should include them into your application. If you have maven project, below dependencies should be added in pom.xml file or else you will get following error in JSP pages – eclipse Can not find the tag library descriptor for "https://java.sun.com/jsp/jstl/ core"

JSTL jar是特定于容器的,例如在Tomcat中,我们需要在项目构建路径中包括jstl.jarstandard.jar jar文件。 如果它们在容器lib目录中不存在,则应将它们包含在应用程序中。 如果您有Maven项目,则应在pom.xml文件中添加以下依赖项,否则您将在JSP页面中收到以下错误-eclipse 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标签 (JSTL Tags)

Based on the JSTL functions, they are categorized into five types.

基于JSTL函数,它们分为五种类型。

  1. JSTL Core Tags: JSTL Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc. To use JSTL core tags, we should include it in the JSP page like below.
    <%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>

    In this article, we will look into important JSTL core tags.

    JSTL核心标签 :JSTL核心标签提供对迭代,条件逻辑,捕获异常,URL,转发或重定向响应等的支持。要使用JSTL核心标签,我们应将其包括在JSP页面中,如下所示。

    在本文中,我们将研究重要的JSTL核心标签。

  2. JSTL Formatting and Localisation Tags: JSTL Formatting tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles. We can include these jstl tags in JSP with below syntax:
    <%@ taglib uri="https://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

    JSTL格式和本地化标签 :提供JSTL格式标签,用于通过语言环境和资源包格式化数字,日期和i18n支持。 我们可以使用以下语法在JSP中包含这些jstl标记:
  3. JSTL SQL Tags: JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc. Using JSTL SQL tags we can run database queries, we include these JSTL tags in JSP with below syntax:
    <%@ taglib uri="https://java.sun.com/jsp/jstl/sql" prefix="sql" %>

    JSTL SQL标记 :JSTL SQL标记提供了与关系数据库(例如Oracle,MySql等)交互的支持。使用JSTL SQL标记,我们可以运行数据库查询,我们在JSP中将这些JSTL标记包括以下语法:
  4. JSTL XML Tags: JSTL XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation. Syntax to include JSTL XML tags in JSP page is:
    <%@ taglib uri="https://java.sun.com/jsp/jstl/xml" prefix="x" %>

    JSTL XML标签 :JSTL XML标签用于处理XML文档,例如解析XML,转换XML数据和XPath表达式求值。 在JSP页面中包含JSTL XML标签的语法是:
  5. JSTL Functions Tags: JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc. Syntax to include JSTL functions in JSP page is:
    <%@ taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn" %>

    JSTL函数标签 :JSTL标签提供了许多我们可以用来执行常见操作的函数,其中大多数用于字符串操作,例如字符串连接,拆分字符串等。在JSP页面中包括JSTL函数的语法是:

Note that all the JSTL standard tags URI starts with https://java.sun.com/jsp/jstl/ and we can use any prefix we want but it’s best practice to use the prefix defined above because everybody uses them, so it will not create any confusion.

请注意,所有JSTL标准标签URI均以https://java.sun.com/jsp/jstl/开头,我们可以使用所需的任何前缀,但是最佳做法是使用上面定义的前缀,因为每个人都使用它们,因此它将不会造成任何混乱。

JSTL核心标签 (JSTL Core Tags)

JSTL Core Tags are listed in the below table.

下表列出了JSTL核心标记。

JSTL Core TagDescription
<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核心标签 描述
<c:out> 要在JSP页面中编写内容,我们也可以将此EL与该标签一起使用
<c:import> 与<jsp:include>相同或包含指令
<c:重定向> 将请求重定向到另一个资源
<c:set> 在给定范围内设置变量值。
<c:删除> 从给定范围中删除变量
<c:catch> 捕获异常并将其包装到一个对象中。
<c:if> 与EL一起使用的简单条件逻辑,我们可以使用它来处理<c:catch>中的异常
<c:选择> 简单的条件标签,为互斥条件操作建立上下文,用<c:when>和<c:otherwise>标记
<c:何时> <c:choose>的子标签,如果其条件评估为'true',则包括其主体。
<c:否则> <c:choose>的子标签,如果其条件评估为'false',则包括其主体。
<c:forEach> 用于集合的迭代
<c:forTokens> 用于由定界符分隔的令牌的迭代。
<c:param> 与<c:import>一起使用以传递参数
<c:url> 创建带有可选查询字符串参数的URL

JSTL教程 (JSTL Tutorial)

Let’s see some of the JSTL core tags usages in a simple web application. Our project will include a Java Bean and we will create a list of objects and set some attributes that will be used in the JSP. JSP page will show how to iterate over a collection, using conditional logic with EL and some other common usage.

让我们看看一个简单的Web应用程序中的一些JSTL核心标记用法。 我们的项目将包括一个Java Bean,我们将创建一个对象列表并设置一些将在JSP中使用的属性。 JSP页面将显示如何使用带有EL的条件逻辑和其他一些常见用法对集合进行迭代。

JSTL教程– Java Bean类 (JSTL Tutorial – Java Bean Class)

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类 (JSTL Tutorial – Servlet Class)

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页面 (JSTL Tutorial – JSP Page)

home.jsp code:

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>

Now when we run application with URL https://localhost:8080/JSTLExample/HomeServlet, we get response as in below image.

现在,当我们使用URL https://localhost:8080/JSTLExample/HomeServlet运行应用程序时,我们将收到如下图所示的响应。

In above JSTL example, we are using c:catch to catch the exception within the JSP service method, it’s different from the JSP Exception Handling with error pages configurations.

在上面的JSTL示例中,我们使用c:catch来捕获JSP服务方法中的异常,这与具有错误页面配置的JSP异常处理不同。

That’s all for a quick roundup of JSTL tags and example of JSTL core tags usage.

这就是快速汇总JSTL标签和JSTL核心标签用法的示例。

Reference: JSTL Wikipedia Page

参考: JSTL Wikipedia页面

翻译自: https://www.journaldev.com/2090/jstl-tutorial-jstl-tags-example

jstl-api jstl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值