JSP standard tag library -- jstl

Source: Internet
Author: User
Tags http redirect

JSP standard tag library -- jstl

Jstl is a set of custom tag libraries that implement common functions in Web applications.

Jstl has five categories:

1. Core tag Library

2. internationalized/formatted tag Library

3. Database tag Library

4. xml tag Library

5. Function tag Library (functions)

Core tag Library

The core tag library mainly includes general tags, condition tags, iteration tags, and URL-related tags.

In the beginning of JSP using core tag library, add code: % @ tagliburi = "http://java.sun.com/jsp/jstl/core” prefix =" C "%

General labels:

<C: Out> label

-Outputs the expression results to the current jspwriter object. Its function is similar to JSP expressions <% = %> or El expressions $ {}.

-Syntax
<C: Out value = "value" [default = "Default Value"] [escapexml = "True | false"]/>
<C: Out value = "value" [escapexml = "True | false"]> <! -- If it is true, escape the <,>, ",", and & characters in the output content, convert to & lt, & gt, & #039, & #034, and & amp -->
Default Value
</C: Out>

Eg:

<%

Request. setattribute ("user", "Lisi ");

%>

<C: Out value ="Helloworld"/><Br/>

<C: Out value ="$ {Username}"Default ="XXXX"/><Br/>

<C: Out value ="<H1>Hello !!!</H1>"Escapexml ="False"/><Br/>

<C: Out value ="$ {Username}"> Text content </C: Out>

<C: Set> label

-The <C: Set> label is used to set variables in various fields of the JSP page, or to set attributes of a Java. util. map object or a JavaBean object.

-Syntax

• <C: Set Value = "value" Var = "name" [scope = "range"]/> Save the value to a variable named name, at the same time, the name variable is saved to the selected range. For example,
<C: Set Value = "sunwk" name = "username" Scope = "session"/>

The range is: Page, request, session, and application.

• <C: Set Var = "name" [scope = "range"]>
Value
</C: Set>
Save the content in the TAG body to the variable named name, and save the name variable to the selected scope. For example,
<C: Set Var = "username" Scope = "session">
Sunwk
</C: Set>

• <C: Set Value = "value" target = "target" property = "propertyname"/> Save the value to the propertyname attribute of the target object.

• <C: set target = "target" property = "propertyname">
Value
</C: Set>
Save the content of the TAG body to the propertyname attribute of the target object.

Eg:

<C: Set Value ="Sunwukong"Var ="User"Scope ="Request"> </C: Set>

$ {User} <br/>

<C: Set Var ="User"Scope ="Request">

Zhubajie

</C: Set>

$ {User} <br/>

<%

MapMap =New Hashmap();

Request. setattribute ("map", MAP );

%>

<! -- Set key-value pairs for a set within a specified range -->

<C: Set Value ="Tangseng"Property ="AAA"Target ="$ {Map}"> </C: Set>

$ {Map. AAA} <br/>

<! -- Assign values to attributes in JavaBean -->

<%

Person P =NewPerson ();

Request. setattribute ("person", P );

%>

<C: Set Value ="Jerry"Property ="Name"Target ="$ {Person}"> </C: Set>

$ {Person. name}

<C: Remove> label

-<C: Remove> the tag is used to remove variables in the specified field range on the JSP page.

-Syntax
<C: Remove Var = "name" [scope = "range"]/>

<C: Remove Var ="Person"Scope ="Request"/>

<C: Out value ="$ {Person. name}"Default ="No such person"/>

<C: Catch> tag

-<C: Catch> the tag is used to capture the exception object thrown by the content nested in the TAG body and save the exception information to the variable.

-Syntax
<C: catch [Var = "name"]>
Body content
</C: Catch>
Put codes that may cause exceptions in this tag. If an exception occurs, save the exception information to the name variable.

<! -- Catch exception: place the content that may occur in the catch

, The exception object will be saved in a certain scope as an attribute,

VaRIs the content of the Memory exception object in the specified range -->

<C: Catch Var ="Myex">

<%

IntI = 10/0;

%>

</C: Catch>

Exception:<C: Out value ="$ {Myex}"> </C: Out><Br/>

The cause of the exception is:<C: Out value ="$ {Myex. Cause}"> </C: Out><Br/>

Exception message:<C: Out value ="$ {Myex. Message}"> </C: Out>

Exception stack track:<C: Out value ="$ {Myex. stacktrace}"> </C: Out>

Condition tag

<C: If> label

-The <C: If> label is used for condition determination. Its function is similar to <% IF (Boolean) {}%> in JSP.

Note: You can use Relational operators such as = ,! Such Relational operators can also be replaced by eq, NE, LT, le, GT, and Ge.

Eg:

<C: If test ="$ {Username = NULL}"Var ="XX"Scope ="Request">

This user has not logged on

</C: If>

<C: Choose>, <C: When>, and <C: otherwise> labels

-The <C: Choose> label is used to provide context for conditional selection. It must be used with the <C: When> and <C: otherwise> labels.

-<C: When> is a sub-tag of <C: Choose>. <C: When> has a test attribute with a Boolean value, if the value of test is true, the content of the <C: When> label body is executed.

-The <C: otherwise> tag has no attribute and must appear as the last branch of the <C: Choose> tag.

Eg:

<%

Request. setattribute ("Age", 18 );

%>

<C: Choose>

<C: When test ="$ {Age> 70}">

Elderly

</C: When>

<C: When test ="$ {Age <= 70 & age> 35}">

Middle-aged

</C: When>

<C: When test ="$ {Age <= 35 & age> 18}">

Young people

</C: When>

<C: When test ="$ {Age <= 18 & age> 0}">

Teenagers or children

</C: When>

<C: otherwise>

Input error

</C: otherwise>

</C: Choose>

Iteration Tag:

<C: foreach> label

-<C: foreach> A tag is used to iterate over a set containing multiple objects, repeat its TAG body, or repeat a fixed number of iterations.

Note:

The items attribute is not a mandatory attribute, but the begin and end attributes must be used when the items attribute is not used.

Ø varstatus: used to save information related to the existing traversal count. For example, if varstatus = "I", the corresponding information is put into the variable named I. The I variable has four attribute values: Index, Count, first, and last.

Index is the index of the current operation member.

Count is the total number of Operation members.

First indicates whether the operated member is the first member.

Last indicates whether the operated member is the last member.

Eg:

<C: foreach begin ="1"End ="10"Step ="1">

Helloworld

</C: foreach><Br/>

Eg2: according to the table type, the color of the odd and even rows is different. When the cursor is placed on the column, the entire column changes color.

<Head>

<Style type ="Text/CSS">

. Even{

Background-color:# Ffff99;

Color:# Ff0000;}

. Odd{

Background-color:# FFFF00;

Color:# Ff0000;}

Tr: Hover{

Background-color:# 0000ff;}

</Style>

</Head>

<Body>

<%

ListList =New Arraylist();

List. Add ("AAA ");

List. Add ("BBB ");

List. Add ("CCC ");

List. Add ("DDD ");

List. Add ("eee ");

List. Add ("fff ");

List. Add ("ggg ");

List. Add ("hhh ");

List. Add ("III ");

List. Add ("jjj ");

Request. setattribute ("list", list );

%>

<! -- Varstatus stores status objects. status information during iteration does not support El expressions. status information has four attributes: Index, Count, first, and last -->

<Table border ="1"Width ="50%">

<C: foreach items ="$ {List}"Var ="Str"Begin ="1"End ="9"Step ="1"Varstatus ="Status">

<! -- Status. Count % 2! = 0 determine whether there are odd rows, odd rows -- even, even rows -- odd -->

<Tr class ="$ {(Status. Count % 2! = 0 )? 'Even': 'odd '}">

<TD >$ {STR} </TD>

<TD >$ {status. Index} </TD>

<TD >$ {status. First} </TD>

<TD >$ {status. Last} </TD>

<TD >$ {status. Count} </TD>

</Tr>

</C: foreach>

</Table>

</Body>

<C: fortokens> label

-<C: fortokens> A tag is used to browse all members of a string. Its members are separated by delimiters.

Eg:

<C: fortokens items = "A: B: C: D" delims = ":" Var = "token">

<C: outvalue = "$ {token}"/> <br>

</C: fortokens>

Note: The usage of this tag is equivalent to the Java. util. stringtokenizer class. Separate the string "A: B: C: D" with the ":" separator, And the token is the string generated by repeating to the current split.

URL-related tags:

<C: Import> label: other static or dynamic files can be included in the JSP page. The difference between <JSP: Include> and <JSP: Include> is that <JSP: Include> can only contain files in the same web application as itself. <C: import> not only can contain files in the same web application as yourself, but also files in other Web applications or other websites.

Eg:

<! -- If you writeVaRProperty, the content in myjsp. jsp will be included in secondpage. You must use the El expression to display it on this page -->

<C: Import url ="/Myjsp. jsp"Var ="Secondpage"/>

$ {Secondpage}

• <C: URL> tag

-<C: URL> the tag constructs a URL on the JSP page. It provides three functional elements that can be appended with the name of the current servlet context before the URL; you can rewrite URLs for session management. You can perform URL encoding on request parameter names and values. These functions are particularly useful when constructing URLs for J2EE Web applications.

• <C: param> label

-The <C: param> label is used to add request parameters to a URL. The preceding <C: URL>, <C: Redirect>, and <C: the usage of <C: param> has been seen in the import> label.

<% Session. setattribute ("XXXXX", "XXXXX"); %>

 

<%

Session. setattribute ("user", "www ");

%>

<! -- Value build address,VaRAttribute names are stored in the scope, and scope stores the scope -->

<! -- Index. jsp Add/. The detailed path is displayed when it is displayed below -->

<C: URL value ="/Index. jsp"Var ="Strurl"Scope ="Request">

<! -- C: ParamURLAdditional parameter -->

<C: Param name ="Pageno"Value ="2"> </C: param>

</C: URL>

$ {Strurl}

<A href ="$ {Strurl}">Homepage </a>

 <A href ="<C: URL value ='/Index. jsp'/>">Homepage </a>

 

• <C: Redirect> label

-The <C: Redirect> label is used to send an HTTP redirect response to a user's browser. It is equivalent to the sendredirect () method function of javax. servlet. http. httpservletresponse in jstl.

Eg:

<! --URLRedirected address -->

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

</C: Redirect>

 

 

 

Related Article

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.