2021-03-04-jstl tag library summary

What is it

  • JSTL is an extension of apache to EL expressions (that is, JSTL depends on EL)
  • JSTL is a tag language, it is very convenient to use, it is the same as JSP action tags
  • JSTL is not a built-in tag of jsp, so we need to guide the package ourselves

Why does it appear

  • Too much Java code is nested in JSP, which is inconvenient to maintain

how to use

  • Import the jar package of jstl
  • Use the taglib directive to import the tag library

Core tag library

  • core
最核心的一个标签库,也是最常用的,重点掌握
导入标签库:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
  • fmt
格式化标签库,可以用来格式化时间和数字
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  • fn
提供了大量的函数,可以求出集合的长度等等
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

fn

  • ${fn:length(list)}: Get the length of the list

fmt

label Description Attributes
<fmt:formatDate value="${date}" pattern=“yyyy-MM-dd”/> Format the time, format the Date type time into a string type pattern="yyyy-MM-dd" formatting parameters
<fmt:formatNumber value=“123.33333” maxFractionDigits=“3”/> Format the number, keep 3 digits after the decimal point pattern=#.##

core

label Description
<c:out value=”${name}” default=”default value”/>
<c:out value=”admin” default=”default value”/> When the value is empty, the default value is output
<c:set var=”a” value=”hello” scope=”session”/> Add data whose name is a and value is hello to the session.
<c:remove var=“a” scope=”page”/> Delete the data whose name is a in pageContext
<c:url value="/"/> Output context path: /project name/
<c:url value="/" var=“a” scope=“request”/> Assign the result that should have been output to the variable a. The scope is request
<c:url value="/abc" var=“name” scope=“request”><c:param name=“username” value=“123”></c:param><c:param name=“password” value=“abc”></c:param></c:url> Output: /project name/abc?username=123&password=abc If the parameter contains Chinese, then the URL encoding will be used automatically!
<c:if test="${a==3}">a==3</c:if> The test attribute of the if tag must be a boolean value, if the value of test is true, then the content of the if tag is executed, otherwise it is not executed
<choose> The choose tag corresponds to the switch statement in Java. When the test of the when tag is true, the content of this when will be executed. When all the when tags of the test are false, the content of the otherwise tag will be executed
<c:forEach> Loop through the collection and map

Guess you like

Origin blog.csdn.net/qq_41270550/article/details/113845031