jsp jstl
JSP Standard Tag Library(JSTL) is a standard library of readymade tags. The JSTL contains several tags that can remove scriplet code from a JSP page by providing some ready to use, already implemented common functionalities.
JSP标准标记库(JSTL)是现成的标记的标准库。 JSTL包含几个标记,这些标记可以通过提供一些现成的,已实现的通用功能来从JSP页面中删除代码片段代码。
JSTL is divided into 5 groups:
JSTL分为5组:
JSTL Core: JSTL Core provides several core tags such as if, forEach, import, out etc to support some basic scripting task. Url to include JSTL Core Tag inside JSP page is →
JSTL Core: JSTL Core提供了几个核心标记,例如if,forEach,import,out等,以支持一些基本的脚本任务。 在JSP页面中包含JSTL核心标记的网址是→
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSTL Formatting: JSTL Formatting library provides tags to format text, date, number for Internationalised web sites. Url to include JSTL Formatting Tags inside JSP page is →
JSTL格式: JSTL格式库提供了用于格式化国际化网站的文本,日期,数字的标记。 在JSP页面内包含JSTL格式标记的网址是→
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
JSTL sql: JSTL SQL library provides support for Relational Database Connection and tags to perform operations like insert, delete, update, select etc on SQL databases. Url to include JSTL SQL Tag inside JSP page is →
JSTL sql: JSTL SQL库支持关系数据库连接和标记,以在SQL数据库上执行诸如插入,删除,更新,选择等操作。 在JSP页面中包含JSTL SQL Tag的网址是→
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
JSTL XML: JSTL XML library provides support for XML processing. It provides flow control, transformation features etc. Url to include JSTL XML Tag inside JSP page is →
JSTL XML: JSTL XML库提供对XML处理的支持。 它提供流控制,转换功能等。在JSP页面内包含JSTL XML Tag的URL为→
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
JSTL functions: JSTL functions library provides support for string manipulation. Url to include JSTL Function Tag inside JSP page is →
JSTL函数: JSTL函数库提供对字符串操作的支持。 在JSP页面中包含JSTL功能标签的网址是→
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
JSP JSTL核心库 (JSP JSTL Core Library)
The JSTL core library contains several tags that can be used to eliminate the basic scripting overhead such as for
loop, if...else
conditions etc from a JSP Page. Let's study some important tags of JSTL Core library.
JSTL核心库包含几个标记,这些标记可用于消除JSP页面中的for
循环, if...else
条件等基本脚本开销。 让我们研究JSTL Core库的一些重要标记。
JSTL if tag: The if tag is a conditional tag used to evaluate conditional expressions. When a body is supplied with
if
tag, the body is evaluated only when the expression is true. For Example :JSTL if标记: if标记是用于评估条件表达式的条件标记。 当为主体提供
if
标记时,仅当表达式为true时才评估主体。 例如 :<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <c:if test="${param.name == 'studytonight'}"> <p>Welcome to ${param.name} </p> </c:if> </body> </html>
JSTL out tag: The out tag is used to evaluate an expression and write the result to JspWriter. For Example :
JSTL out标签: out标签用于评估表达式并将结果写入JspWriter。 例如 :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <c:out value="${param.name}" default="StudyTonight" /> </body> </html>
The
value
attribute specifies the expression to be written to the JspWriter. Thedefault
attribute specifies the value to be written if the expression evaluates null.value
属性指定要写入JspWriter的表达式。default
属性指定如果表达式评估为null时要写入的值。JSTL forEach tag: This tag provides a mechanism for iteration within a JSP page. JSTL
forEach
tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :JSTL forEach标记:该标记提供了一种在JSP页面内进行迭代的机制。 JSTL
forEach
标记的工作方式与Java技术的for循环增强功能类似。 您可以使用此标记来迭代现有的项目集合。 例如 :<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <c:forEach var="message" items="${errorMsgs}" > <li>${message}</li> </c:forEach> </body> </html>
Here the attribute items has its value as an EL expression which is a collection of error messages. Each item in the iteration will be stored in a variable called message which will be available in the body of the forEach tag.
在此,属性项具有其值为EL表达式的值,该表达式是错误消息的集合。 迭代中的每个项目都将存储在一个名为message的变量中,该变量将在forEach标记的主体中可用。
JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the
when
tag evaluates to true, then the content withinwhen
tag is evaluated, otherwise the content within theotherwise
tag is evaluated.JSTL何时选择其他方式标记:这些是用于实现条件操作的条件标记。 如果
when
标签的测试条件评估为true,则评估when
标签内的内容,otherwise
评估otherwise
标签内的内容。We can also implement
if-else-if
construct by using multiple when tag. The when tags are mutually exclusive, that means the first when tag which evaluates to true is evaluated and then, the control exits thechoose
block. If none of the when condition evaluates to true, thenotherwise
condition is evaluated. For Example我们还可以通过使用多个when标签来实现
if-else-if
构造。 when标签是互斥的,这意味着将对第一个评估为true的when标签进行评估,然后控件退出choose
块。 如果没有条件计算为真时,则otherwise
评估条件。 例如
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
<c:choose>
<c:when test="${status.count %2 == 0 }">
<p> Divisible by 2 : ${tutorial.key} </p>
<br/>
</c:when>
<c:when test="${status.count %5 == 0 }">
<p > Divisible by 5 : ${tutorial.key} </p>
<br/>
</c:when>
<c:otherwise>
<p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>
JSTL import tag:
< c:import>
tag is used to dynamically add the contents from the provided URL to the current page, at request time. The URL resource used in the< c:import>
url attribute can be from outside the web Container. For Example :JSTL导入标记:
< c:import>
标记用于在请求时将提供的URL中的内容动态添加到当前页面。< c:import>
url属性中使用的URL资源可以来自Web容器外部。 例如 :<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <c:import url="http://www.example.com/hello.html">> <c:param name="showproducts" value="true"/> </c:import> </body> </html>
JSTL url tag: The JSTL url tag is used to store a url in a variable and also perform url rewriting when necessary. For Example
JSTL URL标签: JSTL URL标签用于将URL存储在变量中,并在必要时执行URL重写。 例如
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <a href='<c:url value="/home.jsp"/>' > Go Home </a> </body> </html>
JSTL set tag: The JSTL set tag is used to store a variable in specified scope or update the property of JavaBean instance. Following is the example of setting the name property of a Student bean :
JSTL set标记: JSTL set标记用于在指定范围内存储变量或更新JavaBean实例的属性。 以下是设置Student bean的name属性的示例:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <c:set target="student" property="name" value="${param.name}" /> </body> </html>
JSTL catch tag: The JSTL catch tag is used to handle exception and doesn't forward the page to the error page. For Example :
JSTL catch标记: JSTL catch标记用于处理异常,并且不会将页面转发到错误页面。 例如 :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Tag Example</title> </head> <body> <c:catch> <% int a = 0; int b = 10; int c = b/a; %> </c:catch> </body> </html>
jsp jstl