JSTL core label details

1. What is a JSTL tag

  • JSTL is a collection of custom tag libraries in java.
  • JSP tag library (JSTL) is a collection of JSP tag libraries, which encapsulates the general core functions of JSP applications.
  • According to the functions provided by JSTL tag, it can be divided into five categories:

    Core label
    Format label
    SQL tag
    XML tag
    JSTL function

2. JSTl environment construction

  • Download the jar package of jstl (import it into the project)

    Binary package downloaded from Apache's standard tag library (Jakarta taglibs standard current. Zip).
    Download address: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/

  • If it is a maven management project (just introduce relevant dependency opportunities and download automatically)
    #jstl dependent address in mvnrepository
  • Here we take maven dependency as an example
    <!-- https://mvnrepository.com/artifact/jstl/jstl -->
  • Introducing jar package in JSP page
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

3. Why use JSTL tags

  • Realize the reuse of JSP page code

    Based on the principle of tag library, the code with high repetition rate supports reuse and improves efficiency.

  • The readability of writing JSP pages is strong

    It looks like XML, which is convenient for the front end to check and participate in modification.

  • Take a simple example to illustrate why JSTL tags should be used

  • Using JSP to write login jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <form action="login.jsp" method="post">
        <input type="text" name="user" value="${param.user}" />
        <input type="submit" value="Submit" />
        if(request.getParameter("user") != null){
                "Welcome users"
         <% }else if(request.getParameter("user").equals("admin")){ %>
                "Welcome administrator"
  • Using JSTL tag to write login jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <form action="app3.jsp" method="post">
        <input type="text" name="user" value="${param.user}" />
        <input type="submit" value="Submit" />
    <c:if test="${param.user == 'user'}">
        <c:out value="Welcome users"></c:out>
    <c:if test="${param.user == 'admin'}">
        <c:out value="Welcome administrator"></c:out>
  • The effect of the two is the same (distinguish the easy to use)

4. There are 13 core tag libraries of JSTL:

The function of < C: import > is basically the same as that of the include instruction in the three instructions. They all specify a JSP page with an absolute or relative path url to expose it to the specified position of the current page.

4.1. Core labels can be divided into four categories in terms of function:

4.2 usage of out tag

  • Output constant.

    You can assign values directly in the value attribute.

  • Output variable

    When the variable does not exist, you can output the default value with the default attribute,
    You can also control the output format of escape characters through escape XML;
    var sets the variable to store the value.

    <%--Output constant(value Direct assignment)--%>
    <c:out value="I am out Output label"></c:out>
    <%--Output variable(value Values can be obtained from the scope)--%>
    <c:out value="${sessionScope.name}"></c:out>

4.3 usage of set tag

  • Save the value into the scope.

    Values can be stored in a specified range in the form of variables.
    scope sets the range of stored values;
    Value specifies the content of the stored value;
    var sets the stored variable.

    <c:set scope="session" value="Li Si" var="name"></c:set>
  • Save the value into the properties of the JavaBean.

    The target attribute specifies the object of the javabean.
    The property property specifies which property to assign to the javabean object.
    Value specifies the content where the value is stored.

    <%--Introduce entity class instantiation object--%>
    <jsp:useBean id="user" class="com.ebuy.pojo.User"></jsp:useBean>
    <%--For instanced objects name Attribute assignment--%>
    <c:set target="${user}" property="name" value="Zhang San"></c:set>

4.4 usage of remove tag

  • Removes variables within the specified scope

    The var attribute is required
    The scope attribute is required
    If the same variable exists in different scopes, you can specify which scope variable to delete through the scope attribute

    <c:remove scope="session" var="name"></c:remove>

4.5 usage of catch tag

  • catch tag

    catch tags can contain error prone JSTL tags (logic).
    The var attribute defines the variable name of the error message output.

    <!--catch label  -->
    <c:catch var="error">
        <c:set target="user" property="name" value="Wang Wu"></c:set>
    obtain catch Error message in tag:<c:out value="${error}"></c:out><br>

4.6 usage of if tag

  • Used to control branching conditions.

    The test attribute is used to store judgment conditions. It is generally written in EL expressions.
    The var attribute specifies the name used to store whether the judgment result type is true or false.
    The scope attribute is used to store the scope of the var attribute.

    <c:if test="${param.score>=90}" var="boolean" scope="session"></c:if>

4.7 usage of choose, when and otherwise labels:

  • Usually these three labels are used together.

    The < C: choose > tag is nested outside the < C: when > and < C: otherwise > tags and used as a parent tag.
    The < C: choose > tag and the < C: when > tag can also be used in combination.

    <c:set var="score" value="99" scope="request"></c:set>
        <c:when test="${score>90 and score<=100}">
        <c:when test="${score>80 and score<=90}">
        <c:when test="${score>70 and score<=80}">
        <c:when test="${score>60 and score<=70}">

4.8 usage of forEach label

  • Traverse the elements in the collection according to the loop conditions.

    var sets the variable name to store the elements taken from the collection (there must be no default value).
    items specifies the collection to traverse (there must be no default value).
    begin, and are used to specify the start and end of traversal (with default).
    Step specifies the step size of the cycle (with default value)
    varStatus describes the status of elements in the begin and end subsets through index, count, first and last status values.

    <c:forEach var="A variable that stores the current traversal element"
               items="Set to be traversed"
               begin="Start traversing element index value"
               end="End traversal of elements index value"
               step="Number of interval elements per traversal in partial traversal"
               varStatus="Specifies a variable to store the state value of the current element">
        <%--operation var Elements represented by variables<br>--%>
  • Instance operation

    <table border="1">
        <c:forEach var="i" begin="1" end="10" step="1" varStatus="j">

  • Interlaced color change

    <%--Interlaced discoloration--%>
    <table border="1">
        <c:forEach var="i" begin="1" end="10" step="1" varStatus="j">
            //jstl tags can be nested in html tags
            <tr <c:if test="${j.index%2==0}"> style="background-color: #3a52a8" </c:if></tr>

    jstl tags can be nested in html tags

  • forEach traverses the list set

        List list = new ArrayList();
        User user1 = new User();
        user1.setUserName("Zhang San");
        User user2 = new User();
        user2.setUserName("Zhang San");
        User user3 = new User();
        user3.setUserName("Zhang San");
    <table border="1">
            <td>full name</td>
        <C:forEach items="${requestScope.list}" var="u">

  • forEach traverses the Map set

        Map<String,String> map = new HashMap<>();
        map.put("1001","Zhang San");
        map.put("1002","Li Si");
        map.put("1003","Wang Wu");
    <table border="1">
            <td>full name</td>
        <c:forEach items="${requestScope.map}" var="m">

4.9. forTokens tag

  • Used to browse the string and intercept the string according to the specified string.

    items specifies the string to be iterated
    delims specifies the delimiter to use
    var specifies the element used to store traversal

    <c:forTokens items="${requestScope.zifu}" delims="," var="z">

4.10 usage of import label

  • Other static or dynamic files can be included in this jsp page.

    The difference from < jsp: include >:
    < jsp: include > can only contain files in the same web application.
    And < C: import > can contain files in other web applications, even resources on the network.
    URL the URL path of the imported resource
    context other web projects under the same server must start with "/"
    var stores the contents of the included file in String type
    JSP scope of Scope var variable
    charEncoding the encoding format of the imported file
    varReader stores the contents of the contained file in Reader type

    <%@ include file="footer.jsp"%>
    <c:import url="footer.jsp"></c:import>

4.11 usage of redirect tag

  • This tag is used to redirect the request, and the specified parameters can be added to the url.

    url specifies the address of the redirected page, which can be an absolute address or relative path of string type.
    context is used to import pages from other web applications.

    <c:redirect url="test.jsp"></c:redirect>

4.12 usage of url tag

  • This tag is used to dynamically generate a url of String type. It can be used together with the < C: param > tag or html tag to realize hyperlink.

    Value indicates the url path value
    var stores the path value of the url in a variable
    Scope indicates the scope of var variable

    <c:url value="http://www.baidu.com" var="baidu">
        <%--Carry parameters--%>
        <c:param name="name" value="zs"></c:param>
        <c:param name="sex" value="2"></c:param>
    <a href="${baidu}">Click to enter Baidu home page</a>

4.13 fmt format label (for understanding)

  • This label can format the date

  • Attribute introduction:

     value The time or date used to format
     type Specifies whether the date or time is formatted,Or both are value ranges:date,time,both
     pattern Custom formatting styles
     dateStyle Formatting style for date
     timeStyle Format style of time
     timeZone Specify the time zone to use
     var Store formatted results
     scope Specify the scope of storage

    Import fmt tag library
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

        Date d = new Date();
    <fmt:formatDate value="${d}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate>

Keywords: Java jstl

Added by jotate on Thu, 03 Feb 2022 05:25:40 +0200