Study notes 7: JSP and JSTL (below)

JSTL

Use of labels

java Server Pages Standard Tag Libray (JSTL): JSP standard tag library, a collection of custom tag libraries, used to solve some common problems, such as iterating a mapping or collection, conditional testing, XML processing, and even databases and accessing databases, etc. operating.
Note: All label operations can only be domain objects .

According to different functions, it is divided into five sub-categories :

    Core (核心库)  标签库的URI:http://java.sun.com/jsp/jstl/core  常用前缀:c

    I18N(国际化)   标签库的URI:http://java.sun.com/jsp/jstl/fmt 常用前缀:fmt

    SQL            标签库的URI:http://java.sun.com/jsp/jstl/sql  常用前缀:sql

    XML            标签库的URI:http://java.sun.com/jsp/jstl/xml 常用前缀:x

    Functions      标签库的URI:http://java.sun.com/jsp/jstl/functions  常用前缀:fn

Let's talk about the commonly used important tags in the JSTL tag library, such as several tags such as iterative collection.

Core tag library :

Core (core library) URI of the tag library: http://java.sun.com/jsp/jstl/core
contains common tasks of web applications, such as looping, expression assignment, basic input and output, etc...

Format tag library :

I18N (internationalization) tag library URI: http://java.sun.com/jsp/jstl/fmt is
used to format and display data, such as: formatting dates in different regions, etc...

To use the JSTL tag library on the JSP page, you must use the taglib instruction to import the related library in the following format:

<%@taglib uri="" prefix=""%>

E.g:

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

Also need to import two jar packages jstl.jar and standard.jar, because I use maven, so I need to add two dependencies, as follows:

	<!--jstl表达式依赖-->
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
    </dependency>
    <!--standard标签库-->
    <!-- https://mvnrepository.com/artifact/taglibs/standard -->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

Conditional Action Label

Output different results according to different values. For example, in Java, if, if else, and switch statements are used for processing, jstl also provides related tags: if, choose, when, otherwise.

If tag

    <%--
        if标签
            格式:
                <c:if test="${条件判断表达式}" var="限域变量名(也就是存放结果的变量)" scope="把结果放在哪个域">
                    ……
                </c:if>

		注意:
		1. 标签操作的一般都是域对象
		2. if标签没有else
		
    --%>
<%
    request.setAttribute("num",10);
%>


例:-------------------------------------------------------------------------------------------

<c:if test="${num>0}" var="result" scope="request">
    数值大于0
</c:if>
    
    从request域里取出结果变量
    ${result}

coose, when, and otherwise tags

Equivalent to switch and case in java, otherwise equivalent to default in switch statement.

The format is as follows:

<c:choose>
    <c:when test="${num == 1}">
        1
    </c:when>
    
    <c:when test="${num == 2}">
        2
    </c:when>
    
    <c:when test="${num == 3}">
        3
    </c:when>
    
    <c:otherwise>
        其他
    </c:otherwise>
    
</c:choose>

Iteration label

The first type: equivalent to a for loop

<c:forEach var="i" begin="0" end="10" step="2">
    ${i}<br>
</c:forEach>

result:
Insert picture description here

The second type: equivalent to foreach loop,
equivalent to for example:

for(String str : list){
    
    

}

jstl: items: the data to be looped var: used to accept the members currently traversed

<c:forEach items="${list}" var="str">
    ${str}
</c:forEach>

Guess you like

Origin blog.csdn.net/qq_40492885/article/details/115294816