The usage and difference of jstl and El expressions in "turn" JSP

Source: Internet
Author: User
Tags arithmetic operators coding standards control label map class tld

For the relationship between Jstl and El, this problem for beginners JSP friends, estimation is a problem, below to detail the Jstl and El expression of their relationship, as well as Jstl and El some related concepts!

El Related Concepts
Jstl is generally used in conjunction with El Expressions to implement Java code snippets that do not appear in JSPs. So let's learn the El expression first.

El is primarily used to look up data in the scope and then perform simple operations on them; it is not a programming language, not even a scripting language. Usually works with the JSTL tag to represent complex behavior with simple and convenient symbols.

El Basic Format

El expression format: delimited with dollar sign ($), content included in curly braces ({});
For example: ${logininfobean.suser}

In addition, you can combine multiple expressions with static text to construct dynamic property values through string literals;
For example: Hello {logininfobean.suser} ${logininfobean.spwd}

El syntax composition-identifier
An El expression consists of identifiers, accessors, literals, and operators.

Identifiers are used to identify the data objects that are stored in the scope. El has 11 reserved identifiers, corresponding to 11 El implicit objects. In addition to the 11 implicit objects, it is assumed that all other identifiers are used to identify scope variables.

Identifier
Cases:
${ABC} is equivalent to <%=pagecontext.findattribute ("abc")%>
${og_1} <%=pagecontext.findattribute ("Og_1")%>
... That is, the identifier inside {} represents the name of the data in the scope except for 11 reserved words.

The Requestscope in ${requestscope} is one of 11 El implicit objects that no longer represents data in the scope, but rather represents the request scope;

El Hidden Objects
PageContext PageContext instance corresponds to the processing of the current page
Pagescope the map class associated with the name and value of the page scope property
Requestscope the map class associated with the name and value of the request scope property
Sessionscope the map class associated with the name and value of a session-scoped property
Applicationscope the map class associated with the name and value of the application scope property
Param Map class that stores the primary values of request parameters by name
Paramvalues the Map class that stores all the values of the request parameter as a String array
Header store the Map class for the main value of the request header by name
Headervalues the Map class that stores all the values of the request header as a String array
A Map class in which cookies are stored by name for the cookie that accompanies the request
Initparam Map class for storing WEB application context initialization parameters by name

El accessor
An element that an accessor uses to retrieve an attribute or collection of an object.

Accessors: Get related data through the "[]" or "." Symbols

Cases:
${userbean.suser} or ${userbean["Suser"}
Gets the value of the Suser attribute in the output bean;
${mctype["id"}//Gets the value of the key in the map corresponding to the ID;

El operator
Operators allow the 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)
Validation operator Empty
Validation operator (empty): Especially useful for validating data. The empty operator takes a single expression as its variable (that is, ${empty input}) and returns a Boolean value that indicates that the result of evaluating the expression is not an "empty" value. An expression that evaluates to Null is considered empty, that is, a collection or array with no elements. The empty operator also returns true if the parameter is the result of a String evaluation of length zero.

El text
The literal represents a fixed value-a number, a character, a string, a Boolean, or a null value.

In an EL expression, a number, a string, a Boolean value, and a null can all be specified as literal values. Strings can be bounded by single or double quotation marks. Boolean values are specified as True and False

What is Jstl?
JSTL (JSP standard tag LIBRARY,JSP standards Tag library) is an ever-improving open source JSP tag library that is maintained by Apache's Jakarta team. The JSTL1.0 consists of four custom tag libraries (core, format, XML, and SQL) and a pair of common tag library validators. The core tag library provides custom actions to manage data by limiting the scope of variables, as well as performing iterations and conditional actions on page content. It also provides tags for generating and manipulating URLs. The Format tag library defines the operations that are used to format data, especially numbers and dates. It also supports the internationalization of JSP pages using localized resource bundles. The XML library contains tags that manipulate the data represented by XML, and the SQL Library defines the operations that are used to query the relational database. Two JSTL tag library validators allow developers to enforce coding standards in their JSP applications.

If you want to use JSTL, you must refer to the Jstl.jar and Standard.jar two packages.

Why do you use Jstl

Our JSPs are very handy for developing information presentation pages, and you can embed Java code (scriptlet, expressions, and declarations) code to implement related logical controls. Look at the procedure below. However, this can lead to the following problems:
JSP maintenance difficulty increased;
Error prompt is not clear, not easy to debug;
The Division of labor is not clear, (that is, JSP developers are artists, programmers);
Finally increase the development cost of the program;
To solve the above problems, you can use the custom tag library, which allows JSP developers to reduce the need for scripting elements, or even jstl them, to avoid the associated maintenance costs. Make the division of labor more clear.

if Else {%><p>welcome, guest!</p><%}%>

Jstl is generally used in conjunction with EL, so look at El first.

JSTL deployment
There are two ways of deploying JSTL in EE applications:
Existing engineering deployments
Two packages of Jstl.jar and Standard.jar to existing projects
Under the Webroot/web-inf/lib directory
The relevant. tld files are tested in the existing Project Webroot/web-inf directory;

You can also use eclipse in an existing
On-Project deployment
Deploy directly when new projects are created
Using the Jstl-core tag library in JSP
The 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>
Adding in the JSP file
<%@ 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"/>

Operation scope variable label for core
Gets the variable in the output scope.
<c:out > Properties: Value [Default] [EscapeXML]
Defining variables in scope
<c:set > Properties: Value var [scope]
To delete a variable in a scope
<c:remove > Properties: var [scope]

Condition control label for Core
Single branch condition
<c:if > Properties: Test [var] [scope]
Multi-branch conditions
<c:choose >
<c:when > Properties: Test
<c:otherwise >

Other labels for core
The output is converted into a URL:
<c:url > Properties: Value [Context] [var] [scope]
and <jsp:include > Similar for content that contains other pages:
<c:import > Properties: URL [context] [charencoding] [var] [scope]
redirect
<c:redirect > Properties: URL [Context]
Used in conjunction with <c:url><c:import><c:redirect> for parameter transfer
<c:param > Properties: Name Value

Loop control label for Core
Implementing a simple loop
<c:foreach > var= ' item ' begin= ' 5 ' end= ' step= ' 2 ' varstatus= '
Implementing iterations (Traversal)
<c:foreach > items= ' var= ' item ' varstatus= '

The Varstatus property sets a scope variable similar to the Var, except that the object that contains the run state is stored in the varstatus scope variable, which includes the following properties:
Current index count first last begin end step

Simple loop

var= "I" step= "1" begin= "1" end= ">" <br></c:forEach></body>

Loop iteration

var= "McBean" items= "${mclist}" varstatus= "Mcstatus" ><br></c:foreach></body >

Using the Jstl-format tag library in JSP
Format is used in JSPs:
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>

Adding in the JSP file

<%@ taglib prefix= "FMT" uri= "Http://java.sun.com/jstl/fmt"%>

Use

<fmt:formatdate value= "" pattern= "Yyyy-mm-dd"/>

Format Common Tags
Formatted output Date:

var pattern

Type value:
short:10/19/00 6:07 PM
MEDIUM:OCT, 6:07:01 PM
Long:october, 6:07:01 PM MDT
Full:thursday, October, 6:07:01 PM MDT
Cases:

<fmt:formatdate value= "" pattern= "Yyyy/mm/dd"/>

Formatted output number:
<fmt:formatNumber> value var pattern
Cases:

<fmt:formatnumber value= "" pattern= "###.##"/>

Format instance

<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "%><%@ taglib prefix=" FMT "uri="/web-inf/fmt.tld "%>

Here we have a certain understanding of the relationship between Jstl and El, as long as we often use these technologies to believe that the advantages and disadvantages of these technologies can be quickly recognized, compared to rote learning, understanding is the real understanding.

Original address: http://www.cnblogs.com/IttaHac/p/6206900.html

The usage and difference of jstl and El Expressions in the

Go 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.