JSTL tag library

1. JSTL depends on El. To use JSTL, you need to import the jar package of JSTL. JSTL has four libraries:

Core: core library, FMT format (date, number), SQL, XML

2. Import JSTL tag library: first, import the jar package, and then <% taglib prefix = "prefix" URI = "path"% > (eg: version 1.2: <% @ taglib prefix = "C" URI) in the JSP page= http://java.sun.com/jsp/jstl/core %>)

3. The core tag library core - > is commonly referred to as the C tag (usually prefixed with C)

(1) Out and set

< C: out / >: output

< C: out value = "ABC" / > output ABC< c: Out value = ${num} / > same as ${num}

< C: out value = ${num} default = "ABC" / > output ABC when ${num} does not exist

< C: out value = ${num} default = "ABC" escapexml = "false" / > when escapexml is false, "<" and ">" will not be converted. Eccapexml defaults to true

< C: set / >: Set

< C: set var = "num" value = "ABC" / > add data with key of num and value of "ABC" to pagecontext

< C: set var = "num" value = ${ABC} scope = "session" / > add data whose key is num and value is ${ABC} to the session. The optional scope values are: session, page, request and application

(2)remove

< C: remove var = "a" scope = "session" / > delete the domain variable. If no scope is specified, it will be deleted from all domains

(3)url

Value: specify a path, and the project name will be automatically added in front of the path

< C: URL value = "/ index. JSP" / > the actual output is / project name / index jsp

Sub tag: < C: param > is used to add parameters to the back of the URL

  

< C: param name = "username" value = "test" / >

  

The actual output is / project name / index jsp? username=%7F7FWWD%9F

Var: specify the variable name. Once this attribute is specified, the URL will not be output to the page, but saved to the domain variable

Scope: used with VaR

(4)if

The test attribute of the if tag must be a Boolean value. If the value of test is true, the contents of the if tag will be executed, otherwise it will not be executed

  

    

  

(Note: no else)

(5) Switch corresponding to choose

  

< C: when test = "${num < 10}" > less than 10 < / C: when >

< C: when test = "${num > 10 & & num < 20}" > greater than 10 but less than 20 < / C: when >

< C: otherwise > greater than 20 < / C: otherwise >

  

(6)forEach

It is used to loop arrays and collections, and can also loop by counting

Counting method:

I variable from 1 to 100, plus 2 each time

< C: foreach var = "I" begin = "1" end = "100" Step = "2" > (including 100, equivalent to < = 100)

  

  

Assign each element in an array or collection to a variable specified by var

  

    

  

Cycle status:

You can use varstatus to create a loop status variable

  

${vs.count} (number of circular elements)

${vs.index} (subscript of current element)

${vs.first} (is it the first element)

${vs.last} (is it the last element)

${vs.current} (current element)

    

  

4. FMT Library

Format date:

  <% Date date = new Date(); pageContext. setAttribute("date",date); %>

  

Format number:

  <% request. setAttribute("num",3.141592653) %>

< FMT: formatnumber value = "${requestscope. Num}" pattern = "0.00" > output two digits after the decimal point (rounded 4 to 5) and fill in zero (if value = 3.1, 3.10 will actually be output)

< FMT: formatnumber value = "${requestscope. Num}" pattern = "#. ##" > no filling

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>