JSTL and EL expressions in jsp: usage and difference; jspjstlel expression

Source: Internet
Author: User
Tags control label map class tld

JSTL and EL expressions in jsp: usage and difference; jspjstlel expression

For the relationship between JSTL and EL, this problem is probably a problem for beginners of JSP. The following describes the relationship between JSTL and EL expressions in detail, and some related concepts of JSTL and EL!

EL concepts
JSTL is generally used together with EL expressions. Therefore, java code segments are not displayed in jsp. So let's first learn EL expressions.

EL is mainly used to find data in the scope, and then perform simple operations on them; it is not a programming language, or even a scripting language. It is usually used together with the JSTL mark and can express complex behaviors with simple and convenient symbols.

EL basic format
EL expression format: it is bounded by the dollar sign ($). The content is included in curly brackets;
Example: $ {loginInfoBean. suser}

In addition, you can combine multiple expressions and static texts to construct dynamic attribute values through String concatenation;
Example: Hello {loginInfoBean. suser }$ {loginInfoBean. spwd}

EL syntax composition-identifier
EL expressions consist of identifiers, accessors, texts, and operators.

Identifiers are used to identify the data objects stored in the scope. EL has 11 retained identifiers, corresponding to 11 EL implicit objects. Except for the 11 implicit object, it is assumed that all other identifiers are used to identify the variables in the scope.

Identifier
Example:
$ {Abc} is equivalent to <% = pageContext. findAttribute ("abc") %>
${Og_1 }<%= pageContext. findAttribute ("og_1") %>
... The identifier in {} represents the name of the data in the scope except for 11 reserved words.

In $ {requestScope}, requestScope is one of 11 EL implicit objects. It does not represent data in the scope, but request scope;

EL hidden object
The pageContext PageContext instance corresponds to the processing of the current page
The Map class associated with the names and values of pagination and page scope attributes
Map class associated with the names and values of requestScope and request scope attributes
Map class associated with the names and values of sessionScope and session scope attributes
Map class associated with the names and values of applicationScope attributes and application scope attributes
Param stores the Map class of the main values of Request Parameters by name
ParamValues uses all the values of request parameters as the Map class stored in the String array.
The Header stores the Map class of the main value of the Request Header by name.
HeaderValues uses all the values in the Request Header as the Map class stored in the String array.
Cookie stores the Map class of the cookie included in the request by name
InitParam stores the Map class of Web application context initialization parameters by name

EL accessors
Accessors are used to retrieve the characteristics or elements of an object.

Accessors: use the "[]" or "." symbol to obtain relevant data.

Example:
$ {UserBean. suser} or $ {userBean ["suser"]}
// Obtain the suser attribute value in the output bean;
$ {McType ["id"]} // obtain the value of the key in the map that corresponds to the id;

EL Operator
Operators allow combination and comparison of data and text.

El operator:
CATEGORY Operators
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
Verification operator (empty): especially useful for data verification. The empty operator uses a single expression as its variable (that is, $ {empty input}) and returns a Boolean value, this Boolean value indicates whether the result of the evaluation of the expression is "null. A null expression is considered null, that is, a set or array without elements. If the parameter is the result of a String with zero length, the empty operator returns true.

EL text
Text represents a fixed value-number, character, String, Boolean, or null.

In EL expressions, numbers, strings, Boolean values, and null can all be specified as text values. A string can be enclosed by single or double quotation marks. Boolean values are specified as true and false.

What is JSTL?
JSTL (JSP Standard Tag Library, JSP Standard Tag Library) is a continuously improved open-source JSP Tag Library maintained by the apache jakarta team. JSTL1.0 consists of four custom tag libraries (core, format, xml, and SQL) and a pair of universal tag library validators. The core tag Library provides custom operations to manage data by limiting the scope of variables, as well as performing page content iterations and conditional operations. It also provides tags for generating and operating URLs. The format tag library defines operations for formatting data (especially numbers and dates. It also supports internationalization of JSP pages using localized resource bundle. The xml library contains some tags that are used to manipulate data expressed in XML, while the SQL database defines the operations used to query relational databases. The two JSTL tag library validators allow developers to enforce encoding standards in their JSP applications.

To use JSTL, you must reference the jstl. jar and standard. jar packages.
Why use JSTL?
JSP is very convenient for developing information display pages. You can also embed java code (scriptlet, expression, and Declaration) code to implement logical control. See the following program. However, this will cause the following problems:
Jsp maintenance becomes more difficult;
The error message is unclear and cannot be easily debugged;
Unclear division of labor (that is, jsp developers are both artists and programmers );
Eventually increase the development cost of the program;
To solve the above problems, you can use a custom tag library. JSTL allows JSP developers to reduce the need for script elements, or even eliminate them, thus avoiding related maintenance costs. Make the Division of Labor clearer.

<% If (session. getAttribute ("user"). equals ("member") {%>
<P> Welcome, member! </P>
<%} Else {%>
<P> Welcome, guest! </P>
<% }%>
JSTL is generally used together with EL, so let's take a look at EL.

JSTL deployment
There are two ways to deploy JSTL in EE applications:
Deploy an existing project
Test the jstl. jar and standard. jar packages to the existing project.
WebRoot/WEB-INF/lib directory
Test the related. tld file to the WebRoot/WEB-INF directory of the existing project;

You can also use eclipse
Project deployment
Directly deploy a new project
Use JSTL-core label library in JSP
Use of core in jsp:
Add in web. xml
<Jsp-config>
<Taglib>
<Taglib-uri> http://java.sun.com/jsp/jstl/core </taglib-uri>
<Taglib-location>/WEB-INF/c. tld </taglib-location>
</Taglib>
</Jsp-config>
Add
<% @ Taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> or <% @ taglib prefix =" c "uri ="/WEB-INF/c. tld "%>
Use <c: out value = "HelloWorld"/>

Core operation scope variable labels
Get the variables in the output scope.
<C: out> attribute: value [default] [escapeXml]
Define variables in the scope
<C: set> attribute: value var [scope]
Delete variable in scope
<C: remove> attribute: var [scope]

Core condition control label
Single Branch Condition
<C: if> property: test [var] [scope]
Multi-branch condition
<C: choose>
<C: when> property: test
<C: otherwise>

Other Core labels
The output URL to be converted:
<C: url> attribute: value [context] [var] [scope]
Similar to <jsp: include>, it is used to include content of other pages:
<C: import> attribute: url [context] [charEncoding] [var] [scope]
Redirection
<C: redirect> property: url [context]
Used with <c: url> <c: import> <c: redirect> to transmit Parameters
<C: param> attribute: name value

Core cyclic control label
Implement a simple Loop
<C: forEach> var = 'item' begin = '5' end = '10' step = '2' varStatus =''
Implement iteration (traversal)
<C: forEach> items = ''var = 'item' varStatus =''

The varStatus and var attributes are similar to the following:
Current index count first last begin end step

Simple Loop
<% @ Page language = "java" contentType = "text/html; charset = GBK" %>
<% @ Taglib prefix = "c" uri = "/WEB-INF/c. tld" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> testjstl1 </title>
</Head>
<Body>
<C: forEach var = "I" step = "1" begin = "1" end = "100">
$ {I} <br>
</C: forEach>
</Body>
</Html>

Loop iteration
<% @ Page language = "java" contentType = "text/html; charset = GBK" %>
<% @ Taglib prefix = "c" uri = "/WEB-INF/c. tld" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> testjstl1 </title>
</Head>
<Body>
<C: forEach var = "mcBean" items = "$ {mcList}" varStatus = "mcStatus">
Current index traversal: $ {mcStatus. index}; Product Name: $ {mcBean. sname};... <br>
</C: forEach>
</Body>
</Html>

Use JSTL-format label library in JSP
Use of format in jsp:
Add in web. xml
<Jsp-config>
<Taglib>
<Taglib-uri> http://java.sun.com/jstl/fmt </taglib-uri>
<Taglib-location>/WEB-INF/fmt. tld </taglib-location>
</Taglib>
</Jsp-config>
Add
<% @ Taglib prefix = "fmt" uri = "http://java.sun.com/jstl/fmt" %>
Use
<Fmt: formatDate value = "" pattern = "yyyy-MM-dd"/>

Common Format labels
Format the output Date:
<Fmt: formatDate> value type var pattern
Type value:
Short: 10/19/00 PM
Medium: Oct 19,200 0 6:07:01
Long: October 19,200 0 6:07:01 MDT
Full: Thursday, October 19,200 0 6:07:01 MDT
Example: <fmt: formatDate value = "" pattern = "yyyy/MM/dd"/>
Format the output number:
<Fmt: formatNumber> value var pattern
Example: <fmt: formatNumber value = "" pattern = "###. ##"/>

Format instance
<% @ Page language = "java" contentType = "text/html; charset = GBK" %>
<% @ Taglib prefix = "fmt" uri = "/WEB-INF/fmt. tld" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> testjstl1 </title>
</Head>
<Body>
<Jsp: useBean id = "curDate" class = "java. util. Date" scope = "page"/>
<Fmt: formatDate value = "$ {curDate}" pattern = "yyyy-MM-dd HH: mm: ss"/> <br>

<Fmt: formatNumber value = "10.32898432" pattern = "#. #"/> <br>
<% Request. setAttribute ("var1", 3.1415926); %>
<Fmt: formatNumber value = "$ {var1}" pattern = "#. #"/> <br>
</Body>
</Html>

Here, we have a certain understanding of the relationship between JSTL and EL. As long as we often use these technologies, we believe that we can quickly recognize the advantages and disadvantages of these technologies, understanding is the true understanding.

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.