[El] El expression

Source: Internet
Author: User

El expression
1. El Introduction
1) syntax structure
$ {Expression}
2) [] and. Operator
El provides two operators:. and [] to access data.
When the attribute name to be accessed contains some special characters, such as. Or? If it is not a letter or number, you must use [].
For example:
$ {User. My-name} should be changed to $ {user ["My-name"]}
If you want dynamic values, you can use [] instead of dynamic values. For example:
Data in $ {sessionscope. User [data]} is a variable.
3) variables
El is easy to access variable data, for example, $ {username }. It means that the name in a certain range is called
Username variable.
Because we do not specify the username range, it will start from page, request, session,
Application range search.
If the username is found on the way, it will be returned directly and will not be found any more, but if all the ranges are not found,
Return null.
Name of attribute range in El
Page pagination
Request requestscope
Session sessionscope
Application applicationscope
4) The 1--el expression is represented by $ {}. It can be used in all HTML and JSP tags to replace the complex Java code in JSP pages.
2--el expressions can operate constant variables and implicit objects. The most common implicit objects include $ {Param} and $ {paramvalues }.
$ {Param} indicates the value of a single string in the returned Request Parameters. $ {paramvalues} indicates a group of returned request parameters.
Value. pagination indicates the variable in the page range. requestscope indicates the variable in the request object. sessionscope indicates the session
The variable in the range. applicationscope indicates the variable in the scope of the application.
3 -- & lt; % @ page iselignored = "true" % & gt; indicates whether the El language is disabled. True indicates that the El language is disabled. False indicates that the El language cannot be disabled.
. The default El language is enabled in jsp2.0.
4 -- the logic expression in El can be displayed. For example, $ {true and false}, the result is a false relational expression. For example, ${5 & gt; 6 }.
If it is a false arithmetic expression, for example, ${5 + 5}, the result is 10.
The variable search range in 5--el is: Page request session application vertex operator (.) and "[]" both
Returns the value of a variable. The difference is that [] can display non-word class variables.
 
2. El implicit object
1) implied objects related to the scope
The El implied objects related to the scope include the following four: pagination, requestscope, sessionscope, and
Applicationscope;
They are basically the same as pagecontext, request, session, and application of JSP;
In El, the four implicit objects can only be used to obtain the range attribute value, that is, getattribute (string name), but cannot be obtained.
Other information.
For example, to obtain the value of the username attribute stored in the session, you can use the following methods:
Session. getattribute ("username") gets the value of username,
Use the following method in El:
$ {Sessionscope. Username}
2) hidden objects related to input
There are two implicit objects related to input: Param and paramvalues, which are special hidden objects in El.
For example, to obtain user request parameters, you can use the following methods:
Request. getparameter (string name)
Request. getparametervalues (string name)
In El, you can use PARAM and paramvalues to obtain data.
$ {Param. name}
$ {Paramvalues. name}
 
3. Other hidden objects
1) Cookie
Jstl does not provide the cookie setting action,
For example, to obtain a value named usercountry in the cookie, use $ {cookie. usercountry }.
Obtain it.
2) header and headervalues
The header stores the data used by the browser and server for communication.
For example, to obtain the version of your browser, use $ {header ["User-Agent"]}.
In addition, it is rare that the same header name may have different values. In this case, you must use headervalues to obtain the value.
These values.
3) initparam
Initparam gets the context parameter for setting the web site)
For example, the general method is string userid = (string) application. getinitparameter ("userid ");
You can use $ {initparam. userid} to obtain the userid name.
4) pagecontext
Pagecontext obtains other details about user requirements or pages.
$ {Pagecontext. Request. querystring} gets the request parameter string
$ {Pagecontext. Request. requesturl} retrieves the request URL, excluding the request parameter string
$ {Pagecontext. Request. contextpath} indicates the name of the Web application of the service.
$ {Pagecontext. Request. Method} Get the HTTP method (get, post)
$ {Pagecontext. Request. Protocol} gets the used protocol (HTTP/1.1, HTTP/1.0)
$ {Pagecontext. Request. remoteuser} obtains the user name
$ {Pagecontext. Request. remoteaddr} obtains the user's IP address
$ {Pagecontext. session. New} checks whether the session is new.
$ {Pagecontext. session. ID} obtains the session ID.
$ {Pagecontext. servletcontext. serverinfo} obtains the service information of the host.
4) condition tag & gt;
1. There are five Arithmetic Operators: +,-, * or $,/or Div, %, or mod.
2. There are six Relational operators: = or EQ ,! = Or NE, & lt; or lt, & gt; or GT, & lt; = or Le, & gt; = or Ge
3. There are three logical operators: & amp; or and, | or ,! Or not
4. Other operators include empty, conditional, and ().
Example: $ {empty Param. name}, $ {? B: c}, $ {A * (B + C )}
5) El functions (functions)
Syntax: NS: function (arg1, arg2, arg3 .... Argn)
NS is the prefix, which must be the prefix of the taglib command.
6) supplement:
& Lt; % @ taglib prefix = "C" http://java.sun.com/jstl/core_rt "& gt; http://java.sun.com/jstl/core_rt" % & gt;
Foreach:
& Lt; C: foreach items = "$ {messages }"
Var = "item"
Begin = "0"
End = "9"
Step = "1"
Varstatus = "Var" & gt;
......
& Lt;/C: foreach & gt;
Out:
& Lt; C: Out value = "/$ {logininfo. Username}"/& gt;
C: Out & gt; outputs the content in the value to the current position. Here, the content of the logininfo object
The username attribute value is output to the current position of the page.
$ {......} Is the Expression Language (EL) syntax in jsp2.0. It defines an expression,
The expression can be a constant (as shown above) or a specific expression Statement (such as in the foreach loop body ).
). Typical cases are as follows:
? $ {Logininfo. Username}
This indicates that the username attribute of the logininfo object is referenced. We can use the "." operator to reference
You can also use "[]" to reference object attributes, for example, $ {logininfo [username]}.
The same effect is achieved with $ {logininfo. Username.
"[]" Indicates that if the attribute name contains special characters, such as "." or "-",
In this case, you must use "[]" to obtain the attribute value to avoid syntax conflicts (avoid as much as possible during system development ).
).
The equivalent JSP script is roughly as follows:
Logininfo =
(Logininfo) Session. getattribute ("logininfo ");
String username = logininfo. GetUserName ();
As you can see, El greatly saves the encoding capacity.
Another problem that is raised here is where El will find the logininfo object.
For expressions such as $ {logininfo. Username },
The variable logininfo is negative. If no value is found, the request, session,
Search within the application range until it is found. If the matched
Return null.
If you need to specify the search range of the variable, you can specify the search range in the El expression:
$ {Pagination. logininfo. Username}
$ {Requestscope. logininfo. Username}
$ {Sessionscope. logininfo. Username}
$ {Applicationscope. logininfo. Username}
In spring, the result data returned by all logical processing units will be placed as the attribute
Return to the httpservletrequest object (for specific implementation, see spring source code .)
Org. springframework. Web. servlet. View. internalresourceview.
Implementation Code of the exposemodelasrequestattributes method), that is, spring
In MVC, The result data objects are requestscope by default. Therefore, in spring MVC,
The following addressing methods should be used with caution:
$ {Sessionscope. logininfo. Username}
$ {Applicationscope. logininfo. Username}
? ${1 + 2}
The result is the expression calculation result, that is, the integer 3.
? $ {I & gt; 1}
If the variable value is I & gt; 1, the return value belongs to the bool type. Compared with the previous example, we can find that El will
Returns different data types based on the calculated results of the expression.
Expressions are written in roughly the same way as expressions in Java code.
If/choose:
& Lt; C: If test = "$ {var. Index % 2 = 0}" & gt;
*
& Lt;/C: If & gt;
The condition is generally an El expression.
& Lt; C: If & gt; The else clause is not provided, which may be inconvenient to use. In this case, we can use & lt; C: Choose & gt;
Tag to achieve a similar purpose:
& Lt; C: Choose & gt;
& Lt; C: When test = "$ {var. Index % 2 = 0}" & gt;
*
& Lt;/C: When & gt;
& Lt; C: otherwise & gt;
!
& Lt;/C: otherwise & gt;
& Lt;/C: Choose & gt;
Similar to the switch statement in Java, & lt; C: Choose & gt; provides a simplified processing method under complex conditions. Its
The & lt; C: When & gt; clause is similar to the case clause and can appear multiple times. The above code outputs the "*" number in an odd number of rows,
The output is "!" in an even row.
Experience: 1. If the El expression cannot be parsed:-& lt; % @ page iselignored = "false" % & gt;
 
I. jstl
 
1. El operator & gt ;;
2) var specifies the variable and assigns the El operation result to the variable value true/false;
3) Scope: Specifies the range of VaR variables;
 
 
6. Iteration tag
Syntax: & lt; C: foreach items = "Collection" Var = "name" varstatus = "status" begin = "int"
End = "Int" step = "Int" & gt;
// Loop body
& Lt;/C: foreach & gt;
Description: 1) items: a set. It uses an El expression;
2) var: variable name, which stores items
3) varstatus: variable that shows the cyclic status
① Index: starts from 0;
② Count: element position, starting from 1;
③ First: True is displayed for the first element;
④ Last: True is displayed for the last element;
4) begin: the initial value of the loop (integer );
5) end: the end of the loop;
6) Step: The step size and the value of the cycle interval;
7. & lt; C: otherwise & gt; tag
Example:
If the user. Wealthy value is true, the user. Wealthy is true.
& Lt; C: Choose & gt;
& Lt; C: When test = "" & gt;
User. Generous is true.
& Lt;/C: When & gt;
& Lt; C: When test = "" & gt;
User. stingy is true.
& Lt;/C: When & gt;
& Lt; C: otherwise & gt;
User. Generous and user. stingy are false.
& Lt;/C: otherwise & gt;
& Lt;/C: Choose & gt;
Note: User. Generous is true is displayed only when the condition user. generous returns 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 {...} So the statements in this form can only use <C: Choose & gt ;,
& Lt; C: When & gt; and & lt; C: otherwise & gt; tags are all done together.
8. C: fortokens & gt; tag
Note:
Items that are cyclic by items are none
Delims Delimiter is none
Begin start condition no 0
End end condition no last project in the Set
Step No 1
VaR indicates whether the variable name of the current project is none
Varstatus: whether the variable that shows the cyclic status is none
Example:
& Lt; C: fortokens items = "A: B: C: D" delims = ":" Var = "token" & gt;
& Lt; C: Out value = ""/& gt;
& Lt;/C: fortokens & gt;
This label is equivalent to the java. util. stringtokenizer class. Here, the string A: B: C: D is separated and cyclically repeated four times,
Token is the string that is split to the current loop.
9. & lt; C: Redirect & gt; tag
Note: The tag redirects the request to another page. It has the following attribute attributes to describe whether the default value is required.
The URL address is none.
Context/followed by the name of the Local Web Application No current application
Example:
& Lt; C: Redirect/'& gt; http://www.yourname.com/login.jsp "/& gt;
Redirect requests to the http://www.yourname.com/login.jsppage, equivalent to response.setredirect
("Http://www.yourname.com/login.jsp ");
10. & lt; C: Param & gt; tag
Note: The & lt; C: Param & gt; label is used to pass a parameter to a redirection or contains page. It has the following attribute Attributions to indicate whether
Required Default Value
The variable name set in the request parameter is none.
Whether the variable value set by value in the request parameter is null
Example:
& Lt; C: Redirect url = "login. jsp" & gt;
& Lt; C: Param name = "ID" value = "888"/& gt;
& Lt;/C: Redirect & gt;
Pass Parameter 888 with ID as the name to the login. jsp page, which is equivalent to login. jsp? Id = 888
11. & lt; FMT: & gt; format tags
(Need to import & lt; % @ taglib prefix = "FMT" http://java.sun.com/jsp/jstl/fmt "& gt; http://java.sun.com/jsp/jstl/fmt" % & gt;
1) format the Date & lt; FMT: formatdate value = "" pattern = "yyyy-mm-dd hh: mm: SS"/& gt;
Value: The date value obtained through the El expression or & lt; % new date () % & gt;
Pattern: the output date format;

2) format the number & lt; FMT: formatnumber
Value = "$ {n}" pattern = "###, ###. ##"/& gt;

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.