5 tag libraries for Java: Core (c), Format (FMT), function (FN), SQL (SQL), XML (x)
SQL, XML library deprecated
Core Tag library (c)
TAGLIB directive
<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
<c:out>
// <c:out> tags for outputting content in JSPs // three properties with value, default, EscapeXML // EscapeXML used to escape reserved XML characters (<, >, ',,, &), set to False to prohibit escaping default = "${defaultvalue}"/>//defaultcan be written in the tag, used to set defaults <c:out value= "${somevalue}" >${defaultValue}</c:out>
<c:if>
// <c:if> Controls whether content within a tag is rendered // with Test, VAR, scope three properties // The test value is false, and everything in the label is ignored <c:if test= "false" > the contents of this will be ignored </c:if>
<c:choose>, <c:when>, <c:otherwise>
// <c:choose> does not contain attributes, only as parent tags for <c:when>, <c:otherwise> The //<c:choose> contains at least one <c:when>, with a maximum of one <c:otherwise>, and only the last label <c:choose>// <c:when> contains only one test property <c:when test= "Somecondition" >if</c:when> <c:when test= "Somecondition" >elseif</c:when> <c:otherwise>Else </c:otherwise></c:choose>
<c:forEach>, <c:forTokens>
//<c:forEach> similar to A for loop in Java//There are items, begin, end, step, Var, varstatus six Properties//for loops in Java for(inti=0;i<100;i++) OUT.PRINTFLN (I);//use <c:forEach> to express//Step Default value is 1, can not write<c:foreach var= "i" begain= "0" end= "step=" 1 ">i</c:forEach>//items represents the collection, array, Map, Iterator, enumeration to traverse//Varstatus represents a variable in a collection<c:foreach items= "lists" varstatus= "Student" >${student.name} ${student.number}</c:forEach>//<c:forTokens> items are strings//<c:forTokens> tags have similar properties to <c:forEach> tags, only one delims attribute, Delims is a delimiter<c:fortokens items= "Google,runoob,taobao" delims= "," var= "name" ><c:out value= "${name}"/></C: Fortokens>
<c:url>
// <c:url> tags can correctly encode URLs, often with <c:param> use // There are value, context, VAR, scope four properties // The context is used to specify the root path // path is/index.jsp<c:url value= "/index.jsp" context= "/"/>// path is/store/item.jsp <c:url value= "/item.jsp" context= "/store"/>//var Specifies the El variable used to create and save the URL result, the default scope is page, Change scope by Scope property <a href= "<c:url value="/index.jsp "var=" Homeurl "scope=" Request "/>" > Test URL </a>
<c:redirect>
// <c:redirect> Tags redirect the browser to a new URL by automatically rewriting the URL, lost <c:redirect> all content, and can be used with <c:param> // URL, context two properties <c:redirect url= "http://www.baidu.com"/>
<c:import>
// you can get the contents of a specific URL resource, which can be saved in a response, string variable, reader variable, and used with <c:param> to specify query parameters in the URL // There are URL, content, charencoding, Var, scope, Varreader six properties // URLs can make relative paths, absolute paths // charencoding, used when the resource does not return Content-type // Scope Specifies the scope of Var // Reader variables in Varreader can only be used in <c:import> tags, not with Var, <c:param> // The following code print Baidu source <c:import var= "Data" url= "http://www.baidu.com"/><c:out value= "${data}"/ >
<c:param>
// <c:param> tags for setting parameters related to URLs // There is a name, value two properties <c:url var= "Myurl" value= "main.jsp" > <c:param name= "name" value= "abc"/> <c:param name= "url" value= "www.baidu.com"/></c:url><a href= "/<c:out value=" ${myurl} "/>" >// output:/main.jsp?name=abc&url=www.baidu.com
<c:set>, <c:remove>
// <c:set> tags for setting variable values and Object properties // with value, Target, property, Var, scope five properties // Modify the value of the object <c:set target= "${someobject}" property= "Someproperty" value= "Hello"/>// Assign a value to a variable <c:set var= "Salary" scope= "session" Value= "${2000*2}"/>//<c:remove> tags for removing a variable, If no scope is specified, all names matching will be removed / var, scope two properties <c:remove var= "Salary"/>
Format Tag library (FMT)
Internationalization and Localization components
Internationalization and localization made up of 3 parts
1. Conversion of different languages between texts
2. Formatting of date, time, currency, percentage
3. Conversion of foreign currency and local currency (usually negligible)
The i18n label is provided in Jstl to handle the conversion of different languages between text, providing formatting of the format tag processing date, time, currency, percentage
// taglib directive <%@ taglib prefix= "FMT" uri= "Http://java.sun.com/jsp/jstl/fmt"%>
i18n Label
<fmt:bundle>, <fmt:setBundle>
i18n tags by <fmt:setLocale>, <fmt:setBundle> specifying resource bundles
Order of Search Resources
The bundle attribute in 1.<fmt:message> specifies the resource bundle
2. Resource bundle embedded in <fmt:bundle>
3. Default Localization Resource Pack
//define the English resource bundle Public classExample_enextendsListResourceBundle { Publicobject[][] getcontents () {returncontents; } Static Finalobject[][] Contents = { {"Count.one", "one"}, {"Count.two", "the other"}, {"Count.three", "three"}, };}//defining the Spanish resource bundle Public classExample_es_esextendsListResourceBundle { Publicobject[][] getcontents () {returncontents; } Static Finalobject[][] Contents = { {"Count.one", "Uno"}, {"Count.two", "Dos"}, {"Count.three", "Tres"}, };}//<fmt:setBundle> for loading the specified resource bundle//properties: BaseName, VAR, scope<fmt:bundle basename= "Com.test.Example_En" > <fmt:message key= "count.one"/><br/> <fmt:message key= "Count.two"/><br/> <fmt:message key= "Count.three"/><br/></fmt:bundle>//Modify Region<fmt:setlocale value= "Es_es"/><fmt:bundle basename= "Com.test.Example_es_ES" > <fmt:message key= " Count.one "/><br/> <fmt:message key=" Count.two "/><br/> <fmt:message key=" Count.three "/> <br/></fmt:bundle>
<fmt:message>
The <fmt:message> tag is used to replace the key in the bundle with the corresponding value//attribute: Key, Bundle, VAR, scope<fmt:setlocale value= "en"/>< Fmt:setbundle basename= "com.test.Example" var= "lang"/><fmt:message key= "Count.one" bundle= "${lang}"/>< Br/><fmt:message key= "Count.two" bundle= "${lang}"/><br/><fmt:message key= "Count.three" bundle= "$ {lang} "/><br/>
<fmt:setLocale>
// <fmt:setLocale> tags are used to store a given region in the locale configuration variable, which is not normally required, and internationalized applications will automatically be locale-specific when the request is forwarded to the JSP // Properties: Value, Variant, scope<fmt:setlocale value= "Es_es"/>
<fmt:requestEncoding>
<fmt:requestEncoding> tags are used to specify the type of form encoding returned to the Web application
Since the servlet handles the request before <fmt:requestEncoding>, the servlet can modify the character encoding, and the modern browser sets the character encoding content type request and response, so this tag is not needed now
Formatting labels
<fmt:timeZone>, <fmt:setTimeZone>
Formatting label Precedence
1. The TimeZone property in the Formatting tab
Inside the 2.<fmt:timezone> tag
3. In the initialization parameter
Time Zone of 4.JVM
// <fmt:setTimeZone> tags are used to copy a time zone object to a specified scope // Properties: Value, VAR, scope<fmt:settimezone value= "America/chicago" var= "zone"/>//<fmt: The timezone> tag is used to specify the time zone for use by other labels // Properties: Value<fmt:timezone value= "${zone}" ></FMT: Timezone>
<fmt:formatDate>, <fmt:parseDate>
//<fmt:formatDate> labels are used to format dates in different ways//Properties: Value, type, Datestyle, Timestyle, pattern, TimeZone, VAR, scope//the value of <fmt:formatDate> must be either an Java.util.Data instance or an El expression, Java.util.Calendar, and Java 8 Date are not supported//type can only be date, time, or BOTH, respectively, indicating only output dates, output times, output dates, and times//Datestyle, Timestyle can only be full, LONG, MEDIUM, short, or DEFAULT//pattern is used for custom format patterns, not recommended<c:set var= "Now" value= "<%=new java.util.Date ()%>"/><p> date format (1): <fmt:formatdate type= "Time"value= "${now}"/></p><p> date format (2): <fmt:formatdate type= "Date"value= "${now}"/></p><p> date format (3): <fmt:formatdate type= "both"value= "${now}"/></p><p> date format (4): <fmt:formatdate type= "both"Datestyle= "Short" timestyle= "short"value= "${now}"/></p><p> date format (5): <fmt:formatdate type= "both"Datestyle= "Medium" timestyle= "Medium"value= "${now}"/></p><p> date format (6): <fmt:formatdate type= "both"Datestyle= "Long" timestyle= "long"value= "${now}"/></p><p> date format (7): <fmt:formatdate pattern= "Yyyy-mm-dd"value= "${now}"/></p>/*Output Date format: Date formatted (1): 11:19:43 date formatted (2): 2018-4-29 date formatted (3): 2018-4-29 11:19:43 Date formatted (4): 18-4-29 11:19 Date formatted (5): 2018- 4-29 11:19:43 Date Format (6): April 29, 2018 11:19 A.M. 43 seconds Date formatted (7): 2019-4-29*/
// <fmt:parseDate> for resolving dates // Properties Same as <fmt:formatDate> <c:set var= "Now" value= "20-10-2015"/><fmt:parsedate value= "${now}" Var= "Parsedempdate" pattern= "dd-mm-yyyy"/><p> resolved date is: <c:out value= "${parsedempdate}"/ ></p>
<fmt:formatNumber>, <fmt:parseNumber>
//<fmt:formatNumber> tags for formatting numbers, percentages, currency//Properties: Value, type, pattern, CurrencyCode, CurrencySymbol, groupingused, Maxintegerdigits,
Minintegerdigits, Maxfractiondigits, Minfractiondigits, VAR, scope//value indicates the number to display//Type can only be number,currency, or percent type//currencycode, CurrencySymbol currency code, currency symbol<c:set var= "balance" value= "120000.2309"/><p> formatted number (1): <fmt:formatnumber value= "${balance}"type= "Currency"/></p><p> formatted numbers (2): <fmt:formatnumber type= "Number"maxintegerdigits= "3" value= "${balance}"/></p><p> formatted numbers (3): <fmt:formatnumber type= "Number"maxfractiondigits= "3" value= "${balance}"/></p><p> formatted numbers (4): <fmt:formatnumber type= "Number"groupingused= "false" value= "${balance}"/></p><p> formatted number (5): <fmt:formatnumber type= "percent"maxintegerdigits= "3" value= "${balance}"/></p><p> formatted number (6): <fmt:formatnumber type= "percent"minfractiondigits= "Ten" value= "${balance}"/></p><p> formatted number (7): <fmt:formatnumber type= "percent"maxintegerdigits= "3" value= "${balance}"/></p><p> formatted numbers (8): <fmt:formatnumber type= "Number"pattern= "###.## #E0" value= "${balance}"/></p>/*number formatting: formatted numerals (1): ¥120,000.23 formatted numerals (2): 000.231 formatted Numbers (3): 120,000.231 formatted Numbers (4): 120000.231 formatted Numbers (5): 23% formatted Numbers (6): 12, 000,023.0900000000% formatted Numbers (7): 23% formatted Numbers (8): 120E3*///<fmt:parseNumber> tags used to parse numbers, percentages, currencies<c:set var= "balance" value= "1250003.350"/><fmt:parsenumber var= "I" type= "number" value= "${balance}"/> <p> Digital Parsing (1): <c:out value= "${i}"/></p><fmt:parsenumber var= "I" integeronly= "true"type= "Number" value= "${balance}"/><p> numeric resolution (2): <c:out value= "${i}"/></p>
function library (FN)
function |
Description |
Fn:contains () |
Tests whether the input string contains the specified substring |
Fn:containsignorecase () |
Tests whether the input string contains the specified substring, is case insensitive |
Fn:endswith () |
Tests whether the input string ends with the specified suffix |
Fn:escapexml () |
Skipping characters that can be tagged as xml |
Fn:indexof () |
Returns the position of the specified string where it appears in the input string |
Fn:join () |
The elements in the array are composited into a string and then output |
Fn:length () |
return string length |
Fn:replace () |
Replaces the specified position in the input string with the specified string and returns |
Fn:split () |
Separates the string with the specified delimiter and then composes a substring array and returns |
Fn:startswith () |
Tests whether the input string starts with the specified prefix |
Fn:substring () |
Returns a subset of a string |
Fn:substringafter () |
Returns a subset of a string after a specified substring |
Fn:substringbefore () |
Returns a subset of the string before the specified substring |
Fn:tolowercase () |
Convert characters in a string to lowercase |
Fn:touppercase () |
To capitalize characters in a string |
Fn:trim () |
Remove the first white space character |
JSP tags and jstl