Summary of the usage of some JSTL core tags in JSP

Time:2020-2-23

1、 JSTL introduction
JSTL (JavaServer Pages Standard Tag Library) is a standard specified by JCP (Java community process), which provides Java Web developers with a standard and general tag function library. And El to replace the traditional method of directly embedding Java programs (scripting) on the page, in order to improve the readability, maintainability and convenience of the program. JSTL is mainly implemented by the Jakarta project organized by Apache. The container must support servlet 2.4 and JSP 2.0 or above.
JSTL download address: http://tomcat.apache.org/taglibs/standard/, the latest version is JSTL 1.2, and this article downloads JSTL 1.1
Installation:
Unzip jakarta-taglibs-standard-1.1.2.zip, and copy jstl.jar and standard.jar under the Lib directory directly to the WEB-INF / lib / directory under the project (if you use MyEclipse, you don’t need to copy these two files, MyEclipse has its own).
To import a label Library:
For example:


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  • Uri: the URI used to import the tag library.
  • Prefix: prefix of the tag library, for example: < C: out >, C is the prefix, which is equivalent to a simple and easy to remember name for the tag.
  • Tagdir: Specifies the path to the label library.

2、 JSTL common Tags:
To use a formatted tag in a JSP page, you must use the <% @ taglib% > instruction,

<%@ taglib prefex=”c” uri=”http://java.sun.com/jsp/jstl/core” %>

1. < C: set > labelCan store variables in JSP scope or JavaBean attributes

There are five formats:

(1)


<c:set var="username" value="value"></c:set>

Making variable names and values

(2)


<c:set var="username" value="value" scope="page|request|session|application"></c:set>

Save the value value to a variable with a scope

(3)

<c:set var="username" scope="page|request|session|application" >

Text content

</c:set>

Store data for text content in a variable with a scope

(4)


<c:set value="value" target="target" property="propertyName"></c:set>

Store the value value in the attribute of the target object.

(5)

<c:settarget="target" property="propertyName">
 
Text content

</c:set>

Store the data of the text content in the properties of the target object

2. < C: out > labelThere are four format syntax for displaying data content

(1)


<c:out value="value"></c:out>

Specify the value to display through the value attribute

(2)


<c:out value="value" escapeXml="true|false"></c:out>

Whether to output the content in value as it is

(3)


<c:out value="value" default="No Data"></c:out>

Set the default value through the default property

(4)

<c:out value="value" escapeXml="true|false">

Text content

</c:out>

Set default values through text content

3.<c:remove>To remove a variable from a specified range

<c:remove var="number" scope="session">

<%@page language="java" contentType="text/html;charset=gb2312" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
Use of the < title > remove tag < / Title >
</head>
<body>
<h1>Use of remove tags</h1>
<hr />
<% -- add variable number, stored in page range --% >
<c:set var = "number" value="${1+2}" scope="page" />
<% -- output variable --% >
number:<c:out value="${pageScope.number}" default="No Data"/>
<% -- remove page range number variable --% >
<c:remove var="number" scope="page" />
<% -- output variable --% >
number:<c:out value="${pageScope.number}" default="No Data"/>
</body>
</html>

4. < C: if > labelUsed to perform process control
There are two formats for < C: if > tags

(1) Without ontology content


<c:if test="condition" var = "varName" [scope="{page|request|session|application}"] />

(2) Having noumenon content

Copy codeThe code is as follows:

< C: if test = “condition” var = “Varname” [scope = “{page| request| session| application}”] > ontology content < / C: if >

5. < C: choose > < C: when > < C: otherwise > label

<%@page language="java" contentType="text/html;charset=gb2312" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
< title > use of choose label < / Title >
</head>
<body>
<h1>Use of choose tag</h1>
<hr />
<c:choose>
<c:when test="${4<6}">
<c:out value="Yes" />
</c:when>
<c:otherwise>
<c:out value="No" />
</c:otherwise>
</c:choose>
</body>
</html>

6. < C: foreach > label
A member for traversing collection objects

<c:forEach [var="username"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >

Local content

</c:forEach>

One is used to make the statement execute circularly for a specified number of times

<c:forEach [var="username"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >

Local content

</c:forEach>

7. < C: fortokens > labelTo split a string based on the specified separator

<c:forTokens [var="varname"] items="stringOfTokens" delims="delimiters" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"] >

Local content

</c:forEach>

8. < C: import > tagStatic or dynamic files can be included in their own JSP pages

<c:import url="url" [context="context"][var="varname"] [scope = "{page|request|session|application}"] [charEncoding="charEncoding"] >

Local content

</c:import>

9. < C: param > label, to pass parameters

10. < C: URL > tag, used to generate URLs

Without parameters

<c:url value="value" [context="context"][var="varname"] [scope = "{page|request|session|application}"] />

Parameterized

<c:url url="url" [context="context"][var="varname"] [scope = "{page|request|session|application}"] >
<c:param />
Label

</c:url>

11. < C: redirect > label, you can jump from one JSP page to another

Without parameters


<c:redirect url="url" [context="context"]/>

Parameterized


<c:redirect url="url" [context="context"]>

< C: param / > tag

</c:redirect>