[JSTL] # simple use of JSTL: core tag c format tag fmt function

1. Core label

The core tag is the most commonly used tag of JSTL. When you want to use it, you can directly import it:

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

1.1 <c:if>

Equivalent to if statement in java

<c:if test="Judgment conditions" var="Variables that store results" scope="var Scope of property">
	Only when the judgment condition is true The statements in this are executed only when


<c:if test="${name == 'Tom'}">
	<h1>hello, Tom!</h1>

1.2 <c:choose> <c:when> <c:otherwise>

It is equivalent to the switch statement in java

	<c:when test="Judgment conditions">
        . . . . . 
    <c:when test="Judgment conditions">
        . . . . . 
    <c:otherwise> <!--When none of the above conditions is true, it shall be implemented otherwise-->
    	. . . . . 

1.3 <c:forEach>

Equivalent to a loop in java

           items="Information to be circulated" 
           step="Step size of each iteration"
           var="The name of the variable currently looped to"
           varStatus="Variable name of loop state"
           [ begin="Start element:0,1..." ]
           [ end="Last element" ]>


<c:forEach items="${students}" var="stud" varStatus="studStatus">
	Serial number: ${studStatus.index}
    name: ${stud.name}

2. Format label

Used to format and output text, date, time and number

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

2.1 <fmt:formatNumber>

Formats the number with the specified format or precision

                  value="Number to display"
                  type="NUMBER, CURRENCY or PERCENT"
                  pattern="Specify a custom formatting mode for output"
                  maxIntegerDigits="The largest number of digits of an integer"
                  maxFractionDigits="Maximum number of digits after decimal point">


<fmt:formatNumber value="${balance}" type="currency"> </fmt:formatNumber>

<fmt:formatNumber value="${balance}" type="number" maxIntegerDigits="3"></fmt:formatNumber>

<fmt:formatNumber value="${balance}" type="number" pattern="###.##E0"></fmt:formatNumber>

2.2 <fmt:formatDate>

Format the date in different ways

                value="Date to display"
                pattern="Custom format"
                type="DATE, TIME or BOTH">


<fmt:formatDate pattern="yyyy-MM-dd" value="${date}"></fmt:formatDate>

2.3 <fmt:parseNumber>

Format a string as a number

3. JSTL function

Most of them are general string processing functions

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

3.1 fn:length()

${fn:length(String or list)}  <!--Get length-->

Other functions are similar:

  • ${FN: contains (original string, substring to find)} does a string contain substrings
  • ${FN: containsignorecase (original string, substring to find)} ignores case
  • ${FN: endswith (original string, substring to find)} determines whether a string ends with the specified suffix
  • ${FN: startswith (< original string >, < prefix of search >)} determines whether a string starts with the specified prefix
  • ${FN: Escape XML (text to escape markup, similar to < ABC >)} skips characters that can be used as XML markup
  • ${FN: indexof (original string, substring)} returns the position of the substring in a string
  • ${FN: join (array, separator)} connects the elements in the array into a string using the specified separator
  • ${FN: replace (original string, replaced string, string to be replaced)} replaces all specified substrings in the string with another string
  • ${FN: split (< string to be separated >, < separator >)} splits a string into an array of substrings with the specified separator
  • ${FN: substring (< string >, < beginindex >, < endindex >)} returns the substring of the specified start and end indexes in the string
  • ${FN: trim (< string >)} removes whitespace at both ends of the string
  • ${FN: substringafter (< string >, < substring >)} returns the part of the string after the substring

Keywords: Front-end html jstl

Added by gtzpower on Thu, 17 Feb 2022 14:01:57 +0200