Jstl core label usage

Source: Internet
Author: User

 

1.1 output calculation result tag <C: Out>

<C: Out value = "value" escapexml = "{true/false}" default = "default"/>

Value: output information. escapexml: The default value is true. Default: the value to be displayed when the value is null.

Column 1:

Page header: Introduced <% @ page prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>

// -- Method 1:

<C: Out value = "xisna is a tuors City"/>

<C: Out value = "$ {null} escapexml =" true "default =" tourist city "/>

// -- Method 2:

<C: Out value = "$ {null}">

Xi an Jiao is a very famous school.

</C: Out>

1.2 set object Value Attribute labels <C: Set>

1. Set the value of a range variable with the Value Attribute

<C: value = "value" Var = "Var" Scope = "Request/session/Application"/>

Ii. Use the value attribute to set the attribute value of an object

<C: Set Value = "value" target = "target" property = "property"/>

3. Set the attribute value of an object for the body content.

<C: set target = "target" property = "property">

Body content

</C: Set>

Event list JSP:

<Body>

<C: Set Var = "name" value = "chengguoyong"/>

<C: Out value = "$ {name}"/>

<Br>

<C: Set Var = "name">

Chengguoyong

</C: Set>

<C: Out value = "$ {name}"/>

</Body>

1.2 delete an object value property tag <C: romove>

Event list JSP:

<Body>

<C: Set Value = "chengguoyong" Var = "name" Scope = "page"/>

<C: Set Value = "chengyong" Var = "name" Scope = "session" default = "Susan"/>

<C: Remove Var = "name" Scope = "page"/>

<C: Out value = "$ {name}"/>

<Br>

<C: Remove Var = "name" Scope = "session"/>

<C: Out value = "$ {name}"/>

1.3 capture exception tags <C: Catch>

Event list JSP:

<Body>

<C: Catch Var = "exception">

<%

Int I = 6;

Int J = 0;

%>

<% = I/J %>

</C: Catch>

<C: Out value = "$ {exception. Message}"/>

<C: Out value = "$ {exception. Cause}"/>

1.4 If condition judgment tag <C: If>

Event list JSP:

<Body>

<C: Set Value = "1.4" Var = "height"/>

<C: If test = "$ {height <1.1}" Var = "result"/> // if the condition is not true, false is returned.

<C: Out value = "$ {result}"/> -- the output is false.

<Br>

<C: If test = "$ {height> 1.1}" Var = "result"> // the content in the execution when the condition is set.

People over 1.1 must buy tickets

<C: If>

1.5 comprehensive application of conditional tags: <C: Choose> <C: When> <C: otherwise>

For example:

<C: Choose>

<C: When test = "condition1">

Body content1

</C: When>

<C: When test = "condition2">

Body content2

</C: When>

......

<C: otherwise>

Condtion1 bock

</C: otherwise>

</C: Choose>

Event list JSP:

<Body>

<C: Set Var = "seaon" value = "Spring"/>

<C: Choose>

<C: When test = "$ {seaon = 'spring'}"> // The output result is spring.

Spring is a fun season.

</C: When>

<C: When test = "$ {seaon = 'summer '}">

Summer is a very hot season.

</C: When>

<C: When test = "$ {seaon = 'autumn'}">

Autumn is a tourist season

</C: When>

<C: otherwise>

Winter is a cold season

</C: otherwise>

</C: Choose>

1.6 iteration tag <C: foreach>

For example:

Note: items: cyclic project step: cyclic step size (displayed every "few") varstatus: variable that records the cyclic status

<C: foreach [Var = "Var"] items = "items" varstatus = "varstauts"

[Begin = "begin"] [END = "end"] [step = "Step"]>

Body content

</C: foreach>

<C: foreach Var = "I" begin = "1" End = "10" step = "2">

<C: Out value = "$ {I}"/> <br>

</C: foreach>

Output results: 1, 3, 5, 7, 9

1.7 process the tokenstring dedicated iteration tag <C: fortokens>

<C: fortokens> tags are used to process strings separated by a series of specific characters.

Delims: delimiter string set, for example, delims = ","

Note:

<C: fortokens items = "stringtokens" delims = "delims"

[Var = "Var"] [varstatus = "varstatus"] [begin = "begin"] [END = "end"] [step = "Step"]

Body content

</C: fortokens>

C: fortokens attribute description table

Attribute name

Type

Description

Reference el

Items

Array, Collection class, string, and enumeration type

Objects that are cyclically traversed are mostly used for arrays and collection classes.

Yes

Delims

String

Character string delimiter. Multiple separator characters can be separated at the same time (enclosed in quotation marks in sequence. If you want to separate "|" and ",", it is written as delims = "| ,".

No

VaR

String

Variable name (original string to be split)

No

Begin

Int

Cycle start position

Yes

End

Int

Loop termination position

Yes

Step

Int

Cycle step (increment)

Yes

Varstatus

String

Cyclic status variable (times)

No

Example:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>
<HTML>
<Head>
<Title> jstl usage -- C: fortokens | C: set | C: If | C: Out </title>
</Head>
<Body>
<C: Set Var = "sourcestr" value = "Zhang San | Si Li | Wang 'er, Ma Zi | jolian | Jack"/>
Original string: <br> <C: Out value = "$ {sourcestr}"/> <br>
Split: <br>
<C: fortokens Var = "str" items = "$ {sourcestr}" delims = "|," varstatus = "status">
<C: Out value = "$ {status. Count}"/>.
<C: Out value = "$ {STR}"/> !!
<C: If test = "$ {status. Last}">
* ** Implement with C: If ***
<Br> it is divided into <C: Out value = "$ {status. Count}"/> segments.
</C: If>
</C: fortokens>
</Body>
</Html>

1.8 construct a URL tag <C: URL>

<C: URL value = "/Trai. jsp" Var = "my_url"/>

$ {My_url}

1.9 redirect URL tag <C: Redirect>

<C: Redirect url = "Welcome. jsp"/>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.