Simply talk about Jsp/el expressions

Source: Internet
Author: User
Tags arithmetic operators dateformat i18n

1. Expressions language (expression Language, EL)

El was originally defined in the JSTL1.0 specification, and in JSP2.0, El was stripped from the jstl and placed in the JSP specification as part of the JSP2.0 specification and added new features. In JSP pages, using El can simplify access to variables and objects.

syntax : ${expr}

1) "[]" and "." Operator to access data ${user.name} ${user["Name"}
2) Arithmetic operators
+ ${23+5}28
- ${23-5}18
* ${23*5}115
/(DIV) ${23/5} or ${23 Div 5}4.6
% (MoD) ${23%5} or ${23 mod 5}3
3) Relational expressions
= = (eq) Equals
! = (NE) Not equal to
< (LT) Less than
> (GT) Greater than
<= (LE) Less than or equal
>= (GE) Greater than or equal
4) Logical operators
&& (and) And
|| (OR) Or
! (not) Non -
5) Empty operator
${empty A} is used to detect if A is null, or empty
6) Conditional operator
${a? B:C}
7) parentheses
The priority used to change execution ${22* (3-1)}

2. Built-in objects

Use location : ${}

El expression built-in objects: 11 kinds

         PageContext:javax.servlet.jsp.PageContext Object Pagescope: Type is map, which maps the property name and its value in the page range. Used primarily to get the value of a property within a page range Requestscope: The type is map, which maps the property name and its value in the request scope. Used primarily to get the value of a property in the requested scope, unlike the request object, to get the request object ${requestcontext.request} Sessionscope: Type is map, session-scoped properties The name and its value are mapped. Used primarily to get the value of a session-scoped property, unlike the session object, to get the Session object ${requestcontext.session} Applicationscope: Type is map, the application The name of the property within the scope of the order and its value are mapped.          Used primarily to get the value of an application-scoped property. Param: Type is map, which maps the name of the parameter in the request and the individual string value. Used primarily to get the parameter value in the request, equivalent to calling Req.getparameter (name) Paramvalues: The type is map, the name of the parameter in the request and an array of type string containing all the values of the parameter are mapped 。 Used primarily to get the value of a parameter in the request, equivalent to calling Req.getparametevalues (name) header: type map, equivalent to calling Req.getheader (name) he Adervalues: Type map, equivalent to calling req.getheaders (name) Cookie: type map, equivalent to calling GetCookies (), found in the returned array          The first cookie object that matches a name. Initparam: The type is map, which is equivalent to calling Context.getinitparameter (Name 

3. Named Variables

${user} named variable: completed by the Pagecontext.findattribute (user) method, the expression finds the named property user in Page,request,session,application scope order, If there is a find, it will return NULL. You can also use Pagescope,requestscope,sessionscope,applicationscope. Specify range, ${sessionscope.user}

4. JSP standard Tag Library JSTL (JavaServer Pages, Tag libary)

JSP provides the function of the custom label, extended the JSP to the label processing ability, many developers have customized their own application of the tag library for the completion of certain functions, in order to unify the standards, sun company customized a set of tag library specifications, namely Jstl. The goal of JSTL is to simplify the design of JSP pages.
1. Jstl Tag Library
Functional Range Rr.Prefix
Core Http://java.sun.com/jsp/jstl/coreC
i18n Http://java.sun.com/jsp/jstl/fmtFmt
Sql Http://java.sun.com/jsp/jstl/sqlSql
Xml Http://java.sun.com/jsp/jstl/xmlX
Functions Http://java.sun.com/jsp/jstl/functionsFn
2. Configure Jstl
Jstl.jar : Contains the interfaces and related classes defined in the JSTL specification
Standard.jar: Contains the development team's implementation of the Jstl and the TLD files for the 5 tag libraries in the JSTL.
3. Core Tag Library
1) label for general purpose
<c:out> Used to evaluate an expression and output the result to the current Jswwriter object similar to the JSP expression <%=expresesion%> or ${} in El
<c:out value= "Value" >
<c:set> The value used to set the range variable
<c:set value= "value" var= "var" [scope= "{page|request|session|application}"]>
<c:remove> for removing range variables
<c:remove var= "var" [scope= "{page|request|session|application}"]>
<c:catch> is used to catch the exception object thrown by the operation in which it is nested.
<c:catch val= "Exception" >
<%
int i = 5;
int j = 0;
int k = i/j;
%>
</c:catch>
<c:out value= "${exception.message}" >
2) Condition label
<c:if> the ability to implement an if statement in the Java language
<c:if test= "test condition" var= "variable" scope= "var range" >
</c:if>
<c:choose> can only include <c:when><c:otherwise>
<c:when test= "Test conditions" >
<c:otherwise>
Determine if the test condition of the <c:when> label is true, the first test condition is true <c:when> tag body is executed by the JSP container, if there is no <c:when> tag that satisfies the condition, then <c:o therwise> tag is executed


3) Iteration Label
<c:forEach> iterates over a collection that contains multiple objects, repeating its label body.
Property:
Var : variable
Items: Iterate objects
Varstatus: The name of the exported range variable that saved the iteration state
Begin: The index at which to start the iteration
End : Terminates the index of an iteration
Step: The step size of the iteration
<c:forTokens> for the members separated by delimiters in the iteration string
Property:
Var : variable
Items: The string object to iterate
Delims: Specifies the delimiter for the split string
Varstatus: The name of the exported range variable that saved the iteration state
Begin: The index at which to start the iteration
End : Ending the index of an iteration
Step: The step size of the iteration


4) URL-related tags
<c:import> is used to import a URL-based resource. Not only can you import resources under the same Web application in the page, you can also import resources under different Web applications, even from other sites.


Property:
Url : The URL of the resource to import
Context: When using a relative URL to access an external resource, specify its context name
Var : The name of the range variable that was exported that saved the contents of the resource
Scope: The JSP range for VAR, which defaults to page
Charencoding: The character encoding of the imported resource content


<c:url> Construct a URL using the correct URL rewrite rules
Property
Value : The URL to process
Context: When using a relative URL to access an external resource, specify its context name
Var: The name of the range variable that was exported with the processed URL saved
Scope: Var jsp range, default page


For example:
<c:url value= "Login" var= "Myurl" >
<c:param name= "name" value= "${param.name}"/>
<c:param name= "Email" value= "${param.email}"/>
</c:url>
<a href= ' <c:out value= "${myurl}" > ' ></a>


<c:redirect> redirects the client's request to another resource.
Property:
Url : Redirect URL for target resource
Context: When using relative URLs to redirect to another external resource, specify its context name
<c:param> Add request parameters to a URL
Property:
Name: query string parameter names
Value: Values of the parameter

5.i18n Tag Library

Mainly used to write an internationalized Web application, it is divided into two parts, the first part is internationalization, the second part is the date, the time and the number of the format, in the JSP page using the i18n tag library, to use the TAGLIB directive, to specify the reference tag library
<%@ taglib url= "http://java.sun.com/jsp/jstl/fmt" prefix= "FMT"%>
<fmt:formatDate> format dates and times in a regional or customized way
Value Date to format, time
Type Specifies whether the date part or time portion of value is to be formatted or formatted, and the default date
Datestyle predefined formatting styles for dates (Java.text.DateFormat)
Timestyle predefined formatting styles for time (Java.text.DateFormat)
Default 2005-5-27 9:38:21
Short 05-5-27 9:39
Medium 2005-5-27 9:38:21
Long May 27, 2005 09:38 A.M. 21
Full Friday, May 27, 2005 09:38 A.M. 21CST
Pattern Custom format date and time styles, custom styles must use the style syntax defined in the Java.text.SimpleDateFormat class. Once the pattern attribute is specified, the Type,datestyle
The time zone used by the TIMEZONE. Reference Java.util.TimeZone
Var The name of the exported variable that saved the formatted result range
Scope The JSP scope of Var


Simply talk about Jsp/el expressions

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.