JSTL tag of JSP

Part of the reference is from: https://blog.csdn.net/qq_25827845/article/details/53311722

1. Introduction

  JSTL is a collection of custom tag class libraries that can be used for iteration or collection, conditional testing, database manipulation, etc. Is an extension of EL expressions, used in conjunction with EL to make JSP pages almost free of Java code.

2. Use

  Now the version of JSTL is 1.2, and two jar packages need to be imported , one is jstl.jar and the other is standard.jar, which is placed in the lib directory.

Use the specified class library with the taglib instruction  on the page using jstl , and use the core library here: <%@ taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core" % >.

  prefix is ​​the prefix of the tag, which can be any content, but it is recommended to use c, and uri is the uri address of the specified tag library.

3. JSTL tag library

  JSTL has a total of four tag libraries, namely:

  core: The core tag library, the main content.

  fmt: Formatting tag library, where formatting numbers and formatting time are more commonly used.

  SQL and XML are basically not used.

Fourth, the core class library:

  1. out tag

    (1) Format: <c:out value="value" [escapeXml=" true |false"] [default="defaultValue"] /> (underscore indicates default value)

      The value of value can be any character or an EL expression. Note that an error will be reported if there are double quotes in the EL expression, so single quotes should be used instead of double quotes in the EL expression.

      escapeXml defaults to true, indicating that the resulting characters <, >, &, ', " in the value will be converted into the corresponding character entity codes <, etc. This is very important, because if a string contains special characters and is not converted , then it cannot be displayed on the client side, which may be attacked by js scripts.

      default can set a default value, if the value is null, return the default value, if the default value is also null, return an empty string

  2. set tag

    (1) There are a total of four formats, of which one or two are similar, and three or four are similar. Therefore, the first and third types are introduced below:

      <c:set  var="varName" value="value" [scope="{page|request|session|application}"] />

      The value can be a character or the value of an EL expression, and scope is the scope of the scoped variable. The effect is to create a scoped variable named varName.

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

      target is the scope object to which a new value is to be assigned, which must be a JavaBean instance, property is the attribute of the target instance, and value is the specific attribute value assigned. The role is to set the property value of the existing restricted object.

  3. remove tags

    (1) 格式:<c:remove var="varName" [scope="{page|request|session|application}"] />

      The var value is the variable to delete in the scope.

  4. if tag

    (1) 格式:<c:if test="testCondition" [var="varName" scope="{page|request|session|application}"] />

      The test value should return a boolean value, so testCondition can be an EL expression with relational and empty operators. var and scope should be used together, var refers to the name of the scoped variable of the test condition value, the type is Boolean, and the scope attribute is used to specify its scope.

  5. choose、when、otherwise标签

    Equivalent to if...else in Java if...else... The following is an example to illustrate:

    

1 <c:set var="score" value="${param.score }"/>  
2 <c:choose>  
3     <c:when test="${score > 100 || score < 0}">错误的分数:${score }</c:when>  
4     <c:when test="${score >= 90 }">A级</c:when>  
5     <c:when test="${score >= 80 }">B级</c:when>  
6     <c:when test="${score >= 70 }">C级</c:when>  
7     <c:when test="${score >= 60 }">D级</c:when>  
8     <c:otherwise>E级</c:otherwise>  
9 </c:choose>  

  6. forEach tag

    The forEach tag is an iterative action instruction, which is very commonly used in JSP.

    (1) Traverse variables

1 <c:set var="sum" value="0" />  
2 <c:forEach var="i" begin="1" end="10" step ="2">  
3     <c:set var="sum" value="${sum + i}" />  
4 </c:forEach>  
5 <c:out value="sum = ${sum }"/> 

    (2) Traverse the array:

1 <%  
2   String[] names = {"zhangSan", "liSi", "wangWu", "zhaoLiu"};  
3   pageContext.setAttribute("ns", names);  
4 %>  
5 <c:forEach var="item" items="${ns}">  
6     <c:out value="name: ${item}"/><br/>  
7 </c:forEach> 

    (3) Traverse the collection

<%  
    List<String> names = new ArrayList<String>();  
    names.add("zhangSan");  
    names.add("lisi");  
    pageContext.setAttribute("ns", names);  
%>  
<c:forEach var="item" items="${ns}">  
    <c:out value="name: ${item}"/><br/>  
</c:forEach>  

   (4) Traverse Map

1 <%  
2     Map<String,String> stu = new HashMap<String,String>();  
3     stu.put("number", "N_1001");  
4     stu.put("name", "zhangSan");  
5     pageContext.setAttribute("stu", stu);  
6 %>  
7 <c:forEach var="item" items="${stu }">  
8     <c:out value="${item.key }: ${item.value }"/><br/>  
9 </c:forEach>  

  (5) varStatus property

    This property is the name of the limited variable that saves the iteration state. You can get the loop state. For example, you can output the delimiter according to the current number of lines, the last line does not output the delimiter, and so on.

    count: int type, currently traversing the number of elements;

    index: int type, the subscript of the current element; first: boolean type, whether it is the first element;

    last: boolean type, whether it is the last element;

    current: Object type, representing the current item.

Five, fmt class library

  The following describes the common usage of formatted time and formatted numbers.

  1. Format the time

1 <%  
2     pageContext.setAttribute("date", new Date());  
3 %>  
4 <fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss"/>  

  2. Format numbers

1 <%  
2     double num = 4.1;   
3     pageContext.setAttribute("num", num);  
4 %>  
5 <fmt:formatNumber value="${num }" pattern="0.00"/><br/>  
6 <fmt:formatNumber value="${num }" pattern="#.##"/>  

 

    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324933584&siteId=291194637