As a reminder, this article only covers the configuration of JSTL and briefly introduces several commonly used tags in JSTL’s Core and Functions tag libraries. For more detailed content, the blogger will post corresponding hyperlinks.
Note: This tutorial uses JSTL 1.2. If you are using JSTL 1.1 or a previous version, you need to import the jstl.jar and standard.jar packages. JSTL 1.1 and previous versions can be downloaded by clicking the link http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/.
The JSTL specification was developed by Sun Microsystems and implemented by Apache’s Jakarta team. JSTL consists of 5 tag libraries with different functions, including Core, I18N, XML, SQL and Functions;
Configuration JSTL
Note: JSTL is now an integral part of Java EE5. If you develop web applications using an integrated development environment that supports Java EE5 or above, you no longer need to configure JSTL.
Update: 2021/12/05
The use of JSTL under the Maven Web project is divided into the following steps:
-
Import coordinates under POM.xml
<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>
-
Introduce JSTL tag library on JSP page
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
-
Use tags
JSTL download
To use JSTL, you need to introduce JSTL’s JAR package and tag library descriptor file (extension: .tld). The tag library descriptor file contains the definitions, tag names, function classes and various attributes of all tags in the tag library.
- JATL JAR package official download URL: https://tomcat.apache.org/taglibs/standard/
After downloading, you can configure it according to the software you are using.
Eclipse platform
If you are using the Eclipse platform, you need to configure JSTL. The steps to configure JSTL are as follows:
1) Copy JSTL standard implementation
Find taglibs-standard-impl-1.2.5.jar
and taglibs-standard-spec in Tomcat's
file, and then copy it to the \webapps\examples\WEB-INF\lib
directory -1.2.5.ja rWEB-INF\lib
directory of the Web project.
2) Use taglib tag to define prefix and uri reference
If you use the Core tag library, you first need to use the taglib tag to define the prefix and uri reference in the JSP page. The code is as follows:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
If you use the Functions tag library, you first need to use the taglib tag to define the prefix and uri reference in the JSP page. The code is as follows:
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
IDEA platform
1) Copy JSTL standard implementation
Find taglibs-standard-impl-1.2.5.jar
and taglibs-standard-spec in Tomcat's
file, and then copy it to the \webapps\examples\WEB-INF\lib
directory -1.2.5.ja rWEB-INF\lib
directory of the Web project.
2) Copy tlb file
After decompressing the compressed package, copy the tld file that needs to be imported under tld to the WEB-INF directory.
Next, we add the following configuration in the web.xml
file: you can add or delete it according to your needs.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/fmt-rt</taglib-uri> <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core-rt</taglib-uri> <taglib-location>/WEB-INF/c-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/sql-rt</taglib-uri> <taglib-location>/WEB-INF/sql-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri> <taglib-location>/WEB-INF/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/x-rt.tld</taglib-location> </taglib> </jsp-config> </web-app>
To use any library, you must include the
core tag
JSTL core tag is the most commonly used JSTL tag. The syntax for importing the core tag library is as follows:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Tag | Description |
---|---|
Output the result of the expression to the page, similar to <%= ...%> | |
Set the variable or attribute value within the specified range | |
Similar to the Java if statement, used for conditional judgment | |
Similar to the Java switch keyword, it is the parent tag of |
|
The sub-tag of |
|
Similar to Java for, used to iterate information in a collection | |
Similar to Java split, used to separate strings | |
Used to delete data | |
Used to catch exceptions | |
Used to import static or dynamic File | |
is used to pass in parameters | |
Used to redirect the current page to another URL | |
Used to format the URL into a string |
Formatting tag
The JSTL formatting (fmt) tag can convert numbers and dates in a very simple way. The syntax for importing the formatting tag library is as follows.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
Tag | Description |
---|---|
Set the character encoding of the request data | |
Used to set the user localization environment | |
Bind resources | |
Used Call information from the specified resource file | |
Bind resources | |
For formatting numbers, percentages and currencies | |
Used for Parsing numbers, currencies and percentages | |
For formatting dates in different ways | |
Used to convert string type date into date data type | |
Used to specify the time zone | |
Used to set the default time zone |
SQL tag
JSTL SQL tags provide many tags for operating databases (MySQL, Oracle, SQL Server, etc.). Although it is not recommended to use this tag library in large websites, it is often used in small websites. The syntax for importing a SQL tag library is as follows.
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
Tag | Description |
---|---|
Used to configure the data source in the JSP page | |
Query the data in the database | |
Update data in the database | |
Provide dynamic values of date and time | |
Transaction management | |
|
Set dynamic values in SQL statements |
function tag
Most JSTL function (fn) tags are general string processing functions. The syntax for importing the function tag library is as follows:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
Tag | Description |
---|---|
fn:contains() | Used to determine whether a string contains a specified string, case-sensitive |
fn:containsIgnoreCase() | Used to determine a Whether the string contains the specified string, case-insensitive |
fn:endsWith() | is used to determine whether a string ends with the specified suffix Ending |
fn:escapeXml() | Used to escape characters in HTML/XML |
fn:indexOf() | Used to return the starting position of the string in the specified string |
fn:join() | Used to concatenate all elements in the array into a string using the specified delimiter |
fn:length() | Used to return Specify the length of the string |
fn:split() | Used to separate the string into an array of substrings using the specified delimiter |
fn:startsWith() | Used to determine whether a string starts with the specified prefix |
fn :substring() | Used to return the substring of the specified string |
fn:substringAfter() | Used to return The part after the specified substring in the string |
fn:substringBefore() | is used to return the part before the specified substring in the string |
fn:toLowerCase() | Used to convert all characters in the specified string to lowercase |
fn:toUpperCase() | Convert all characters in the specified string to uppercase |
fn:trim() | Used to remove spaces at both ends of a specified string |
custom label
Custom tags are tags defined by users (developers) themselves. Custom tags allow the JSP page to contain no Java code and only contain HTML code and some tags to implement business logic calls.
The advantages of custom labels are as follows:
- Reduce the need and dependency on scripts for JSP pages
- Separate JSP pages and business logic to increase the maintainability of the program
- The same business logic can be called repeatedly, increasing the reusability of the program
The steps to use custom labels are as follows:
- Custom label implementation class
- Write tld tag library description file
- Use custom tags in JSP pages
Custom tag syntax
Use the taglib directive to specify the path to the tld file.
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
Among them: prefix specifies the prefix of the custom label, and uri specifies the path of the tld file.
There are 2 formats for using custom tags:
<prefix:tagname attr1=value1....attrn=valuen />
or
<prefix:tagname attr1=value1....attrn=valuen > tag body </prefix:tagname>
Among them: prefix represents the prefix of the custom tag, tagname represents the name of the custom tag, attr represents the attribute of the custom tag, and value represents the attribute value of the custom tag.
Simple example
Let’s create a simple custom tag
1. Create a Java class for processing tags
Create a HelloTag class that handles tags. The code is as follows:
public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Tag!!!"); } }
The above code rewrites the doTag() method, and the getJspContext() method is used to obtain the JspContext object and pass the content in out.println
to the JspWriter object.
Note: SimpleTagSupport implements the SimpleTag interface, which is a new custom tag interface class added to the JSP 2.X tag library. The interface is extremely simple and provides the doTag() method to handle the logic in custom tags and the tag body. Relatively speaking, JSP 1.
2. Create tld tag library description file
The tld file is described in XML file format with the suffix .tld
, which is used to map the written classes into JSP tags. The tld file is stored in the WEB-INF directory. For convenience of management, a tlds folder can be created in the directory.
The custom.tld code is as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <!-- Tag library version number --> <tlib-version>1.0</tlib-version> <!-- JSP version number --> <jsp-version>2.0</jsp-version> <!-- The prefix of the current tag library --> <short-name>Example TLD</short-name> <tag> <!-- The name of the custom tag, used to use the tag in the page --> <name>Hello</name> <!-- Implementation class path of custom tags --> <tag-class>com.riotian.tag.HelloTag</tag-class> <!-- Text content Text content, if not, use empty to represent --> <body-content>empty</body-content> <!-- Function description of custom tags --> <description>Output content</description> </tag> </taglib>
There can only be a pair of taglib tags in the tld file. There can be multiple tag tags under the taglib tag, and each tag tag represents a custom tag.
Each tag in the tld file will be explained later in the article.
3. Use custom tags
Using custom tags is the same as using JSTL tags. The code for using the Hello tag in the JSP file is as follows.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="ex" uri="../WEB-INF/custom.tld" %> <!DOCTYPE html> <html> <head> <title>Tag.jsp</title> </head> <body> <ex:Hello/> </body> </html> </html>
The running results are as follows:
tld tag library description file
Commonly used tags in tld files are taglib, tag, attribute and variable. The following uses the custom.tld file as an example to introduce its meaning.
1. tag
The
Attributes | Description |
---|---|
tlib-version | Tag library version number |
jsp-version | JSP version number |
short-name | Prefix of the current tag library |
uri | URI address of the custom tag referenced by the page |
name | Custom tag name |
tag-class | Custom tag implementation class path |
description | Custom label function description |
attribute | Self Define the specified attributes of the tag, there can be multiple |
2. tag
The
Attributes | Description |
---|---|
name | Custom tag name |
tag-class | Custom tag implementation class |
body-content | There are 3 values: empty (indicating that there is no tag body), JSP (indicating that the tag body can be added to JSP program code), tagdependent (indicating that the content in the tag body is processed by the tag itself) |
description | Custom label function description |
attribute | Custom label function Specify attributes, there can be multiple |
variable | Variable attributes of custom tags |
3. tag
The
Attributes | Description |
---|---|
name | Attribute name |
description | Attribute description |
required | Specify attribute Whether it is required, default value: false |
rtexprvalue | Whether the attribute value supports JSP expressions |
type | Defines the Java type of the attribute. Default value: String |
fragment | If the attribute is declared, the attribute Value will be treated as a JspFragment |
Pay attention to the order of elements when using attributes of
.
4. label
The
Attributes | Description |
---|---|
declare | Variable declaration |
description | Variable description |
name-from-attribute | The specified attribute name, its value is a variable, the name that can be used when calling the JSP page |
name-given | Variable name (label use variable name) |
scope | The scope of the variable has 3 values: NESTED between the start and end tags, AT_BEGIN from the start tag to End of page, AT_END From after the end tag to the end of the page |
variable-class | The Java type of the variable, default value: String |
Custom label attribute
To set attributes in a custom label, there must be a corresponding setter method in the custom label class. Add the message attribute to the bc:Hello tag. The HelloTag class code is as follows:
public class HelloTag extends SimpleTagSupport { private String message; public void setMessage(String message) { this.message = message; } StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { if (message != null) { // use message from attribute JspWriter out = getJspContext().getOut(); out.println(message); } else{ // use message from content getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } } }
The following uses the
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <!-- Tag library version number --> <tlib-version>1.0</tlib-version> <!-- JSP version number --> <jsp-version>2.0</jsp-version> <!-- The prefix of the current tag library --> <short-name>Example TLD</short-name> <tag> <!-- The name of the custom tag, used to use the tag in the page --> <name>Hello</name> <!-- Implementation class path of custom tags --> <tag-class>com.riotian.tag.HelloTag</tag-class> <!-- Text content Text content, if not, use empty to represent --> <body-content>tagdependent</body-content> <!-- Function description of custom tags --> <attribute> <name>message</name> </attribute> </tag> </taglib>
At this time, you can use the message attribute in the JSP page, as follows:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="ex" uri="../WEB-INF/custom.tld" %> <!DOCTYPE html> <html> <head> <title>Tag.jsp</title> </head> <body> <ex:Hello message="Welcome to RioTian'blog..."/> </body> </html> </html>
Tag body of custom tag
You can include message content in tags like the JSTL tag library. For example, if you include content in a Hello tag, the JSP usage format is as follows:
<ex:Hello> Demo Demo Hello JSP Tap.... </ex:Hello>
Modify the HelloTag class as follows:
public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); @Override public void doTag() throws JspException, IOException { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); //super.doTag(); } }
Modify the custom.tld file as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <!-- Tag library version number --> <tlib-version>1.0</tlib-version> <!-- JSP version number --> <jsp-version>2.0</jsp-version> <!-- The prefix of the current tag library --> <short-name>Example TLD</short-name> <tag> <!-- The name of the custom tag, used to use the tag in the page --> <name>Hello</name> <!-- Implementation class path of custom tags --> <tag-class>com.riotian.tag.HelloTag</tag-class> <!-- Text content Text content, if not, use empty to represent --> <body-content>tagdependent</body-content> </tag> </taglib>
The running results are shown in the figure.