JSTL and EL Basics

Source: Internet
Author: User

EL
Expression Language, Expression Language. It is easier to compile JSP by operating data that exists in PageContext. You do not need to introduce jar packages when using EL, as long as the container supports it.

EL implicit object


Basic use of EL
$ {Ld} is equivalent to querying the ld variable from pagination-requestScope-sessionScope-applicationScope in sequence. If yes, the value is returned. If no value is returned, null is returned. Of course, if the variable location is determined, can be obtained directly, such as $ {requestScope. ld }. In addition to the above implicit object, EL treats all other content as variables in a certain field.

EL syntax
In addition, EL can perform common operations.


Arithmetic Operators +,-, *,/(or div), and % (or mod)
Relational operators = (or eq ),! = (Or ne), <(or lt),> (or gt), <= (or le), and> = (or ge)
Logical operators & (or and), | (or), and! (Or not)
Verification operator empty

EL get attribute Parsing

Assume that the user object has been added to requestScope, and now you want to obtain the name attribute of the user object: use $ {user. name} obtains the name attribute of the user object. The way to obtain the attribute is not through the defined variable private String username. Instead, get is removed based on the getName () method and obtained in lower case by name, so modify username to username1 pair $ {user. the use of name} is unaffected.


EL obtains complex attributes.
Use EL to obtain the name attribute of the group attribute of the user object. You can use $ {user. group. name}; use EL to obtain the array. The second parameter can be $ {array [1]}, and get k1 in map can be $ {map. k1.

JSTL is usually used in combination with EL.


JSTL

JSP Standard Tag Library, JSP Standard Tag Library, is a continuously improved open-source JSP Tag Library. Its use requires container support, java code in JSP can be replaced with elegant labels. JSTL provides more controllable details than EL.

Configure and use
Configuration
To use JSTL, You need to introduce two packages: jstl. jar and standard. jar. Add tags to the JSP file.

[Html]
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
Core
The core library provides common out, set, remove, forEach, and other labels, using instances:


[Html]
<C: choose>
<C: when test = "$ {v1 gt v2}">
V1 is greater than v2
</C: when>
<C: otherwise>
V1 is greater than v2
</C: otherwise>
</C: choose>

<C: choose>
<C: when test = "$ {v1 gt v2}">
V1 is greater than v2
</C: when>
<C: otherwise>
V1 is greater than v2
</C: otherwise>
</C: choose>
Format
The Format Library provides control labels for the Format, for example:

[Html]
Today (default): <fmt: formatDate value = "$ {today}"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "date"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "time"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "both"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "both" dateStyle = "full"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" pattern = "yyyy/MM/dd HH: mm: ss"/> <br/>

Today (default): <fmt: formatDate value = "$ {today}"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "date"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "time"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "both"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" type = "both" dateStyle = "full"/> <br/>
Today (default): <fmt: formatDate value = "$ {today}" pattern = "yyyy/MM/dd HH: mm: ss"/> <br/> you can get:

Summary
In general, through JSTL and EL, JSP page programming can be reduced and optimized in the form of tags. Through the display of JSTL and EL functions, JSP has a single responsibility, the Development Division is clearer.

 

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.