A summary of the usage of some JSTL core tags in JSP

  • 2021-09-24 23:19:40
  • OfStack

1. Introduction to JSTL
JSTL (JavaServer Pages Standard Tag Library) is the standard specified by JCP (Java Community Process), which provides Java Web developers with a standard common tag function library. And EL to replace the traditional method of directly embedding Java program (Scripting) on the page, in order to improve the readability, maintainability and convenience of the program. JSTL is mainly implemented by Jakarta Project organized by Apache, and the container must support Servlet 2.4 and JSP 2.0 or above.
Download address of JSTL: http://tomcat.apache.org/taglibs/standard/, the latest version is JSTL 1.2, and this article downloads JSTL1.1
Installation:
Extract jakarta-taglibs-standard-1. 1.2. zip, and directly copy jstl. jar and standard. jar under the extracted lib directory to WEB-INF/lib/directory under the project (if myeclipse is used, these two files need not be copied, and myeclipse has its own).
Import Tag Library:
For example:


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
uri: uri for importing tag libraries. prefix: The prefix of the tag library, for example: < c:out > c is the prefix, which is equivalent to taking a simple and easy-to-remember name for the label. tagdir: Specifies the path to the tag library.

2. JSTL Common Labels:
To use formatting tags in JSP pages, you must use the < %@ taglib% > Instructions,

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

1. < c:set > Tags can store variables in the scope of JSP or in the attributes of JavaBean

There are five formats:

(1)


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

Make 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 range of scope

(3)


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

 Text content 

</c:set>

Store the data of text content in a variable with a range of scope

(4)


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

Stores the value value in the properties of the target object.

(5)


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

</c:set>

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

2. < c:out > Tags are used to display the contents of data, and their format syntax has four kinds

(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 contents in value as 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 the default value through text content

3. < c:remove > Used to remove the variable of the specified range


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

4. < c:if > Tags are used to perform process control
< c:if > There are two formats for labels

(1) Without ontology content


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

(2) Having ontological content


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

5. < c:choose > < c:when > < c:otherwise > Label


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

6. < c:forEach > Label
A way to traverse the members of a collection object


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

 Local content 

</c:forEach>

1 is used to loop the statement a specified number of times


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

7. < c:forTokens > Label used to split the string according to the specified separator


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

8. < c:import > Tag, which can include static or dynamic files into its own JSP web page


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

9. < c:param > Label for passing parameters

10. < c:url > Tag to generate URL

Without parameters


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

<c:param />
 Label 

</c:url>

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

Without parameters


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

Parameterized


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


<c:param /> Label 

</c:redirect>

Related articles: