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 value will be converted to the corresponding character entity code <, 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标签

    forEach标签是迭代动作指令,在JSP中十分常用。

    (1) 遍历变量

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) 遍历数组:

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) 遍历集合

<%  
    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) 遍历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属性

    这个属性是保存迭代状态的限域变量名称,可以获取循环状态,例如可以根据当前所在的行数来输出分隔符,最后一行不输出分隔符等等等。

    count:int类型,当前以遍历元素的个数;

    index:int类型,当前元素的下标;first:boolean类型,是否为第一个元素;

    last:boolean类型,是否为最后一个元素;

    current:Object类型,表示当前项目。

五、fmt类库

  以下介绍常用的格式化时间和格式化数字的一般用法。

  1. 格式化时间

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

  2. 格式化数字

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=324933612&siteId=291194637