JSP Standard Tag Library: JSTL

Keywords: Java JSP Apache Session

JSTL (JSP Standard Tag Library), JSP standard tag library, can be embedded in JSP pages to complete business logic and other functions in the form of tags.

The purpose of jstl is to replace script code in jsp page as well as el.

JSTL standard tag library has five sub libraries, but with the development, its core library is often used at present

 

1. JSTL download and import

JSTL Download:

Download the JAR package for JSTL from Apache's website. Access

"http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/ ”Download the installation package of JSTL.

jakarta-taglibs-standard-1.1.2.zip, and then unzip the downloaded JSTL installation package. At this time, you can see two JAR files in the lib directory, jstl.jar and standard.jar.     

Among them, the jstl.jar file contains the interfaces and related classes defined in the JSTL specification, the standard.jar file contains the. class file used to implement JSTL and five tag library descriptor files (TLD) in JSTL

Import two jar packages into our project's lib

jsp file import method:

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

 

2. Common tags of JSTL core library

if tag example:

    <%
        request.setAttribute("count", 10);
    %>
    <!-- jstl Labels often interact with el Cooperative use -->
    <!-- test Return of Representatives boolean Expression -->
    <c:if test="${count==9 }">
        xxxx
    </c:if>    

 

for loop example:

    <!-- forEach simulation
        for(int i=0;i<=5;i++){
            System.out.println(i)
        }
     -->
     <c:forEach begin="0" end="5" var="i">
         ${i }<br/>
     </c:forEach>

 

Enhanced for loop:

    <!-- Simulated enhancement for    productList---List<Product>
        for(Product product : productList){
            System.out.println(product.getPname());
        }
     -->
     <!-- items:A collection or array   var:Represents an element in a collection-->
     <c:forEach items="${productList }" var="pro">
         ${pro.pname }
     </c:forEach>

 

Simple application of if tag:

Different information is displayed depending on whether the user is logged in:

    <%
        //Simulated user has successfully logged in
        User user = new User();
        user.setId(100);
        user.setName("Zhang San");
        user.setPassword("123");
        session.setAttribute("user", user);
    %>

 

            <!-- User is not logged in -->
            <c:if test="${empty user}">
                <li><a href="login.jsp">Sign in</a></li>
                <li><a href="register.jsp">register</a></li>
            </c:if>
            <!-- User logged in -->
            <c:if test="${!empty user}">
                <li>${user.name }</li>
                <li><a href="#">Sign out</a></li>
            </c:if>

 

for loop (forEach) label example:

    <%
        //simulation List<String> strList
        List<String> strList = new ArrayList<String>();
        strList.add("qwer");
        strList.add("asdf");
        strList.add("zxcv");
        strList.add("1234");
        request.setAttribute("strList", strList);
        
        //ergodic List<User>Value
        List<User> userList = new ArrayList<User>();
        User user1 = new User();
        user1.setId(2);
        user1.setName("lisi");
        user1.setPassword("123");
        userList.add(user1);
        User user2 = new User();
        user2.setId(3);
        user2.setName("wangwu");
        user2.setPassword("123");
        userList.add(user2);
        application.setAttribute("userList", userList);
        
        //ergodic Map<String,String>Value
        Map<String,String> strMap = new HashMap<String,String>();
        strMap.put("name", "lucy");
        strMap.put("age", "18");
        strMap.put("addr", "China");
        strMap.put("email", "lucy@qq.cn");
        session.setAttribute("strMap", strMap);
        
        //ergodic Map<String,User>Value
        Map<String,User> userMap = new HashMap<String,User>();
        userMap.put("user1", user1);
        userMap.put("user2", user2);
        request.setAttribute("userMap", userMap);
        
        
        
    %>
    
    
    
    <h1>take out strList Data</h1>
    <c:forEach items="${strList }" var="str">
        ${str }<br/>
    </c:forEach>
    
    <h1>take out userList Data</h1>
    <c:forEach items="${userList}" var="user">
        user Of name: ${user.name }------user Of password: ${user.password }<br/>
    </c:forEach>
    
    <h1>take out strMap Data</h1>
    <c:forEach items="${strMap }" var="entry">
        ${entry.key }====${entry.value }<br/>
    </c:forEach>
    
    <h1>take out userMap Data</h1>
    <c:forEach items="${userMap }" var="entry">
        ${entry.key }:${entry.value.name }--${entry.value.password }<br/>
    </c:forEach>

Posted by feyd on Thu, 09 Apr 2020 10:51:00 -0700