[Detailed] EL expression and the use of JSTL tag library

      EL expression and JSTL tag library are the means or methods of JSP and back-end data manipulation.

      Both EL expression and JSTL tag library serve JSP, which simplifies the processing of data in the JSP interface.

      EL expression is to simplify the output of JSP, simplify the way to get the value in the domain.

      JSTL is a collection of built-in objects that judge and traverse JSP, and EL expressions take the value in the domain

      Learning jQuery, Ajax is easier.                          

One, EL (Expression Language) expression

1. The EL expression language is used to simplify the output of JSP, mainly to output the domain content in the built-in objects of JSP.

     The basic syntax of EL expression: ${expression}. For example, save a collection list to request:

List<Student> list = new ArrayList<Student>();
Student stu = new Student();
stu.setName("zhangsan");
stu.setAge(18);
list.add(stu);
request.setAttribute("list",list);

     At this time, use the EL expression to fetch the value    ${list}, which is equivalent to the output in JSP <%=request.getAttribute("list")%>

2. Scope object:

     There are four built-in scope objects in EL expressions. 

    

     They can read the value of the object set by the setAttribute() method of the built-in jsp objects pageContext, request, session, and application, that is, get the value in the field getAttribute(String name).

     Page:PageScope . How to use ${pageScope.request_name}

     Request:RequestScope,使用方式${requestScope.request_name},相当于<%=request.getAttribute("request_name")%>

     Session: SessionScope , the usage method is ${sessionScope.session_name}, which is equivalent to <%=session.getAttribute("session_name")%>

     Application:ApplicationScope,使用方法${applicationScope.application_name},相当于<%=application.getAttribute("application_name")%>

3. The output of EL expression:

    Syntax: ${scope.attribute name.subattribute name}, EL expression supports the output of operation results, the essence is toString(). If the EL expression is empty, the output result is also empty.

Two, JSTL tag library

    jstl is a third-party tag library for JSP, and a third-party jar package needs to be introduced.

    The core tag library (core) is the most important tag library of JSTL, which provides the basic functions of JSTL.

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

    ①Judgment label:

             1) Single branch judgment: <c:if> </c:if>

             2) Multi-branch judgment:

<c:choose> 
     <c:when> 
       代码段                
     </c:when>  
     <c:otherwise>
       代码段
     </c:otherwise>
</:choose>

     ② Traverse the collection

// var 是给items的元素起别名 , items是获取域中的值
<c:foreach var="list" items="${list}">
    
</c:foreach>

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/111603814