Use JSTL today

Source: Internet
Author: User
Tags control label i18n
Jstl Introduction
 
 
JSTL Introduction
Jstl is an open-source JSP tag library that is constantly improved and maintained by Apache's Jakarta team. Jstl can only run on containers that support jsp1.2 and servlet2.3 specifications, such as Tomcat 4.x. However, JSP 2.0 will be supported as a standard.

JSTL consists of two parts: the tag library and EL (Expression Language) Language. The tag library currently supports four types of tags:
Tag URI prefix example
Core http://java.sun.com/jstl/core c <c: tagname...> XML processing http://java.sun.com/jstl/xml x <x: tagname...> i18N capable formatting http://java.sun.com/jstl/fmt fmt <fmt: tagname...> database access (SQL) http://java.sun.com/jstl/ SQL SQL <SQL: tagname...>

Core supports some basic operations in JSP;
XML processing supports processing XML documents;
I18N capable formatting supports internationalization of JSP pages;
Database access (SQL) supports JSP Database operations.

Due to my limited level, this article only introduces Core tags. If you are interested, you can discuss the use and expansion of the other three tags.

El

The EL language is the representation of a JAVA expression from the JSTL output (input.
In JSTL, EL can only be used in attribute values. The EL language can only be called by creating expressions. There are three ways to use expressions in attribute values.

1. The value attribute contains an expression.
<Some: tag value = ""/>
In this case, the expression value is calculated and assigned to the value attribute according to the type conversion rule. For example, <C: Out value = ""/> is an El, which is equivalent to a JSP statement <% = request. getattribute ("username") %> or <% = session. getattribute ("username") %>

2. The value attribute contains one or more attributes, which are separated by text or centered around
<Some: Tag value = "sometext"/>
In this case, the expression is calculated from left to right, the result is converted to a string type (according to the type conversion rules), and the result is assigned to the value attribute.

3. The value attribute only contains text
<Some: Tag value = "sometext"/>
In this case, the value of the string type property is converted to the desired type of the tag according to the type conversion rules.

El Operators
Obtains the attribute values of an object or set.
To obtain attributes in a set, El supports the following two operations:
1. Use the. Operator to obtain attributes with names. For example, the expression indicates the username attribute of the Object User.
2. Use the [] operator to obtain attributes with names or numbers.
The expression and expression have the same meaning.
The expression indicates the first entry of the row set.
Here, user is a class object, and its property username must comply with the standard JavaBean specification, that is, the corresponding getter and setter methods must be defined for the username attribute.

Empty operator (Null Value Check)

Use the empty operator to determine whether an object, set, or string variable is null or null. For example:
True
If the username value in the request parameter list is null, the expression value is true. EL can also directly use the comparison operator to compare with null. Such as true.
Comparison operator

Operator Description
= Or eq Equality check
! = Or ne Unequal check
<Or lt Less than check
> Or gt Greater than check
<= Or le Less than or equal to check
> = Or ge Greater than or equal to check

The numeric and logical operators are the same as those in Java and are not listed.

Core tag Library

1. General labels

<C: Out>
<C: out> A tag is used to display data in JSP. It has the following attributes:

Genus Description Required? Default Value
Value The output information, which can be an EL expression or constant. Yes None
Default Information displayed when value is null No None
EscapeXml If the value is true, special xml character sets are avoided. No True

Example:

Your username is: <c: out value = "default =" guest "/>

Displays the user name. If it is null, guest is displayed.

<C: out value = ""/>

Display the username value obtained from the session;

<C: out value = ""/>

Display the username value. The value is retrieved from the request (PAGE) by default. If the request does not contain an object named username, It is retrieved from the session. If the value is not in the session, it is retrieved from the application (servletcontext) if no value is obtained, it is not displayed.

<C: Set>
<C: set> A tag is used to save data. It has the following attributes:

Genus Description Required? Default Value
Value The information to save. It can be an EL expression or a constant. No
Target The variable name of the attribute to be modified, generally the instance of the javabean No None
Property Javabean attribute to be modified No None
Var Variable for saving information No None
Scope Range of variables for saving information No Page

If the target attribute is specified, the property attribute must also be specified.
Example:
<C: set value = "" var = "test2" scope = "session"/>

Save the value of test. testinfo to Test2 of the session. test is a JavaBean instance and testinfo is the property of the test object.

<C: set target = "" property = "city" value = ""/>

Save the city property value of the object Cust. Address to the variable city.

<C: Remove>
<C: remove> A tag is used to delete data. It has the following attributes:

Genus Description Required? Default Value
Var Variable to be deleted Yes None
Scope Scope of the deleted variable No All scopes, including page, request, session, and application

Example:
<C: remove var = "test2" scope = "session"/>

Delete the Test2 variable from the session.

2. Flow Control label

<C: If>

<C: if> labels have the following attributes:

Genus Description Required? Default Value
Test The condition to be evaluated is equivalent to the condition in the IF (...) {} statement. Yes None
VaR Variable name for storing the condition Result No None
Scope Variable range for saving condition results No Page

<C: Choose>
This tag does not accept any attributes.

<C: When>
<C: when> labels have the following attributes:

Genus Description Required? Default Value
Test Conditions for evaluation Yes None

<C: otherwise>
This tag does not accept any attributes.

Example:

<C: If test = "">
User. Wealthy is true.
</C: If>

If the user. Wealthy value is true, the user. Wealthy is true.

<C: Choose>
<C: When test = "">
User. Generous is true.
</C: When>
<C: When test = "">
User. stingy is true.
</C: When>
<C: otherwise>
User. Generous and user. stingy are false.
</C: otherwise>
</C: Choose>

User. Generous is true is displayed only when the return value of condition user. Generous is true.
User. stingy is true only when the return value of condition user. stingy is true.
All other cases (that is, the values of user. generous and user. stingy are not true) show user. generous and user. stingy are false.

Because JSTL is not like if (){...} Else {...} Therefore, the statements in this form can only be completed using the <c: choose>, <c: when>, and <c: otherwise> labels.

3. cyclic control labels

<C: foreach>
<C: forEach> A tag is used for common data and has the following attributes:

Genus Description Required? Default Value
Items Cyclic Project No None
Begin Start Condition No 0
End End Condition No The last project in the collection.
Step Step Size No 1
VaR Variable name of the current project No None
Varstatus Variable that shows the loop status No None

Example:

<C: foreach items = "" Var = "vector">
<C: Out value = ""/>
</C: foreach>

Equivalent to a java statement
For (INT I = 0; I <vectors. Size (); I ++ ){
Out. println (vectors. Get (I ));
}

Vectors is a java. util. Vector object that stores String data and vector is a String object in the current loop. In fact, vectors can be any object that implements the java. util. Collection interface.

<C: forEach begin = "0" end = "100" var = "I" step = "1">
Count = <c: out value = ""/> <br>
</C: forEach>

Output:
Count = 0
...
Count = 100

<C: fortokens>
<C: forTokens> tags have the following attributes:

Genus Description Required? Default Value
Items Cyclic Project Yes None
Delims Delimiter Yes None
Begin Start Condition No 0
End End Condition No The last project in the collection.
Step Step Size No 1
Var Variable name of the current project No None
VarStatus Variable that shows the loop status No None

Example


<C: forTokens items = "a: B: c: d" delims = ":" var = "token">
<C: out value = ""/>
</C: forTokens>

This label is equivalent to the java. util. StringTokenizer class. Here, the string a: B: c: d is separated for four times and the token is the string to be split to the current one.

4. Import files and URLs

JSTL core tag Library supports <c: import> to contain files, <c: url> to print and format URLs, and <c: redirect> to redirect URLs.

<C: Import>
<C: import> the tag contains the code of another page to the current page. It has the following attributes:

Genus Description Required? Default Value
Url Url of the page to be imported Yes None
Context /Followed by the name of the Local web Application No Current Application
CharEncoding Character set used to import data No ISO-8859-1
Var Variable name that accepts the imported text No Page
Scope Variable range of the input text No 1
VarReader Java. io. Reader variable name used to accept the imported text No None
VarStatus Variable that shows the loop status No None

<C: URL>
<C: url> the tag outputs a url address, which has the following attributes:

Genus Description Required? Default Value
Url Url Yes None
Context /Followed by the name of the Local Web Application No Current Application
Charencoding Character set used to import data No ISO-8859-1
VaR The name of the URL variable that has been processed. The variable stores the URL No Output to page
Scope Variable range of the variable name storing the URL No Page

Example:


<C: Import url = "http://www.url.com/edit.js" Var = "newsfeed"/>

Add the url http://www.url.com/edit.js to the front page and save the url to the newsfeed.

<A href = "<C: URL url ="/index. jsp "/>"/>

Output <a href = "http://www.yourname.com/index.jsp"/> at the current location of the current page, where the http://www.yourname.com is the location of the current page.

<C: Redirect>
<C: redirect> the tag redirects the request to another page. It has the following attributes:

Genus Description Required? Default Value
URL URL Yes None
Context /Followed by the name of the Local Web Application No Current Application

Example:

<C: Redirect url = "http://www.yourname.com/login.jsp"/>

Redirect requests to http://www.yourname.com/login.jsppage, equivalent to response.setredirect ("http://www.yourname.com/login.jsp ");

<C: param>
<C: param> A tag is used to pass a parameter to a redirection or contain page. It has the following attributes:

Genus Description Required? Default Value
Name Variable name set in the request Parameter Yes None
Value Variable value set in the request Parameter No None

Example:

<C: redirect url = "login. jsp">
<C: param name = "id" value = "888"/>
</C: redirect>

Pass Parameter 888 with id as the name to the login. jsp page, which is equivalent to login. jsp? Id = 888

Advantages of JSTL
1. the same interface is provided between application servers, which increases the porting of Web applications between application servers.
2. Simplified JSP and web application development.
3. Reduce the number of scriptlet code in JSP in a unified way, and achieve programs without any scriptlet code. In our company's projects, no scriptlet code is allowed to appear in JSP.
4. Further integration of JSP design tools with Web application development is allowed. I believe there will soon be an IDE development tool supporting jstl. I just want to use it now. It's a good article. Thank you for your great help.




Created at: 11:13:25, modified at: 16:35:32, viewed 4081 times, with 3 comments

 

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.