El expression (Detailed)

Source: Internet
Author: User
Tags arithmetic operators

L-Expressions
1, El Introduction
1) Grammatical structure
${expression}
2) [] with the. operator
EL provides. And [] Two operators to access data.
When you want to access a property name that contains some special characters, such as. or? Other than letters or numbers, be sure to use [].
For example:
${user. My-name} should be changed to ${user["My-name"}
If you want to dynamically fetch values, you can do it with [], and. cannot be dynamically evaluated. For example:
${sessionscope.user[data]} in data is a variable
3) variables
The EL Access variable data method is simple, for example: ${username}. It means to take out a range of names called
The username variable.
Because we do not specify a range of username, it will be sequentially from page, Request, Session,
Application Range Lookup.
If the way to find username, directly back, no longer continue to find, but if the whole range has not been found,
The callback is null.
Name of the attribute range in El
Page Pagescope
Request Requestscope
Session Sessionscope
Application Applicationscope
4) 1--el expression is represented by ${}, which can be used in all HTML and JSP tags instead of the complex Java code in a JSP page.
The 2--el expression can manipulate constant variables and implicit objects. The most commonly used implicit objects are ${param} and ${paramvalues}.
${param} represents the return value of a single string in the request parameter. ${paramvalues} represents a set of return request parameters
The value. Pagescope represents a variable for a page range. Requestscope represents a variable for the Request object. Sessionscope represents a session
The variable within the range. Applicationscope represents a variable that applies scope.
3--<%@ page iselignored= "True"%> indicates whether the El language is disabled, true to suppress. False indicates that
The default enabled El language in JSP2.0.
The 4--El language can display a logical expression such as ${true and false} and the result is a false relationship expression such as ${5>6} knot
The result is a false arithmetic expression such as ${5+5}, which results in 10
The variable search scope in 5--el is: page request session application dot operator (.) and "[]" are both
Represents the value of the get variable. The difference is [] A variable that can display non-speech
2. El Hidden Objects
1) Implied objects related to the range
The El implied objects that are related to a range contain the following four: Pagescope, Requestscope, Sessionscope, and
Applicationscope;
They are basically the same as JSP PageContext, request, session and application;
In El, these four hidden objects can only be used to obtain the range attribute value, that is, getattribute (String name), but cannot get
Other relevant information.
For example: We want to get the value of storing an attribute username in the session, you can use the following methods:
Session.getattribute ("username") obtains the value of username,
In El, the following methods are used
${sessionscope.username}
2) Implicit objects related to the input
There are two hidden objects associated with the input: param and paramvalues, which are the more specific hidden objects in El.
For example, when we want to get the user's request parameters, we can take advantage of the following methods:
Request.getparameter (String name)
Request.getparametervalues (String name)
In El, you can use both Param and paramvalues to get the data.
${param.name}
${paramvalues.name}
3. Other hidden objects
1) Cookies
JSTL does not provide a cookie-setting action,
Example: To get a value in a cookie that has a setting name of Usercountry, you can use ${cookie.usercountry} to
Get it.
2) Header and Headervalues
The header stores the data that the user's browser and server use to communicate
Example: To get the version of the user's browser, you can use ${header["User-agent"}.
In addition, under rare opportunities, it is possible to have different values for the same header name, and you must instead use Headervalues to obtain
These values.
3) Initparam
Initparam get environment parameters for setting Web site (context)
Example: General method String UserID = (string) application.getinitparameter ("userid");
You can use ${initparam.userid} to get the name UserID
4) PageContext
PageContext obtain additional information about user requirements or pages.
${pagecontext.request.querystring} Gets the requested argument string
${pagecontext.request.requesturl} Gets the requested URL, but does not include the request's argument string
${pagecontext.request.contextpath} The name of the Web application of the service
${pagecontext.request.method} Method of getting HTTP (get, POST)
${pagecontext.request.protocol} Get the protocol used (http/1.1, http/1.0)
${pagecontext.request.remoteuser} Get user name
${PAGECONTEXT.REQUEST.REMOTEADDR} Get the IP address of the user
${pagecontext.session.new} to determine if the session is a new
${pagecontext.session.id} Gets the ID of the session
${pagecontext.servletcontext.serverinfo} access to host-side service information
4) Condition label >
1. There are five arithmetic operators: + 、-、 * or $,/or div,% or mod
2. There are six relational operators: = = or EQ,! = or NE, < or LT, > or GT, <= or LE, >=, or GE
3. The logical operator has three:&& or an and, | | Or,!, or not
4. There are three other operators: the empty operator, the condition operator, the () operator
Example: ${empty param.name}, ${a? B:C}, ${a* (B+c)}
5) El function (functions)
Syntax: Ns:function (arg1, arg2, Arg3 .... ArgN)
Where NS is the predecessor name (prefix), and it must be placed with the predecessor name of the TAGLIB directive
6) Supplement:
<%@ taglib prefix= "C" Http://java.sun.com/jstl/core_rt ">http://java.sun.com/jstl/core_rt"%>
Foreach:
<c:foreach items= "${messages}"
Var= "Item"
begin= "0"
End= "9"
step= "1"
Varstatus= "var" >
......
</c:forEach>
Out:
<c:out value= "/${logininfo.username}"/>
C:out> outputs the contents of value to the current position, where the Logininfo object's
The Username property value is output to the current position of the page.
${...} is the syntax of expression Language (EL) in JSP2.0. It defines an expression,
The expression can be either a constant (as above) or a concrete expression (such as in the Foreach Loop body
of the situation). Typical cases are as follows:
? ${logininfo.username}
This indicates that the Username property of the Logininfo object is referenced. We can pass the "." Operator Primer
Object properties, such as ${logininfo[username]}, can also be referenced with "[]" using the object's properties.
and ${logininfo.username} achieved the same effect.
The meaning of the "[]" reference is that if a special character appears in the property name, such as "." Or "-",
At this point, you must use "[]" to get the property values to avoid grammatical conflicts (system development should be avoided as far as possible
The emergence of this phenomenon).
The equivalent JSP script is roughly as follows:
Logininfo Logininfo =
(logininfo) Session.getattribute ("Logininfo");
String username = logininfo.getusername ();
As you can see, El greatly saves the amount of code.
Another question that arises here is where EL will find the Logininfo object, for
${logininfo.username}, the first thing to look for in the current page is the
The variable logininfo is defined, and if it is not found then the request, Session,
Application in the range until it is found. If the match is not found until the end
Variable, NULL is returned.
If we need to specify a scope for the variable, you can specify the search scope in the EL Expression:
${pagescope.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 attribute
To the HttpServletRequest object (the implementation can be found in the spring source
Org.springframework.web.servlet.view.InternalResourceView.
Implementation code for the Exposemodelasrequestattributes method), which means that spring
In MVC, the result data object is requestscope by default. So, in spring MVC,
The following addressing methods should be used with caution:
${sessionscope.logininfo.username}
${applicationscope.logininfo.username}
? ${1+2}
The result evaluates to an expression, which is an integer value of 3.
? ${I>1}
If the value of the variable is i>1, the bool type true is returned. Compared with the above example, it can be found that El Will self-
Returns different data types based on the result of the expression calculation.
Expressions are written in roughly the same way as expressions in Java code.
If/choose:
<c:if test= "${var.index% 2 = = 0}" >
*
</c:if>
The decision condition is generally an El expression.
<c:if> does not provide an ELSE clause, which may be inconvenient when used, at which point we can pass <c:choose>
tag to achieve a similar purpose:
<c:choose>
<c:when test= "${var.index% 2 = = 0}" >
*
</c:when>
<c:otherwise>
!
</c:otherwise>
</c:choose>
A switch statement similar to Java,<c:choose> provides a simplified approach to complex judging conditions. Its
The <c:when> clause resembles a case clause, which can occur more than once. The above code, which outputs the "*" number on odd lines,
And even rows output "!".
Experience: 1, if the El expression cannot be resolved: –<%@ page iselignored= "false"%>
First, JSTL
1. El operator >;
2) var specifies the variable and assigns the result of the El operation to the value of the variable true/false;
3) Scope: Specify the range of var variables;
6. Iteration Label
Syntax: <c:foreach items= "collection" var= "name" varstatus= "status" begin= "int"
end= "int" step= "int" >
Loop body
</c:forEach>
Description: 1) Items: is a collection, with an El expression;
2) Var: variable name, storing items
3) Varstatus: variable showing the loop state
①index: starting from 0;
②count: Element position, starting from 1;
③first: Displays True if it is the first element;
④last: Displays True if it is the last element;
4) Begin: The initial value of the loop (integral type);
5) End: Loop end;
6) Step: Step length, the value of the cycle interval;
7. <c:otherwise> Label
Cases:
User.wealthy is true if the User.wealthy value is true.
<c:choose>
<c:when test= "" >
User.generous is true.
</c:when>
<c:when test= "" >
User.stingy is true.
</c:when>
<c:otherwise>
User.generous and User.stingy are false.
</c:otherwise>
</c:choose>
Description: User.generous is true only if the condition User.generous return value is true.
User.stingy is true only if the condition User.stingy return value is true.
All other cases (that is, the values of user.generous and user.stingy are not true) show all user.generous and
User.stingy is false.
Because Jstl is not shaped as if () {...} else {...} Conditional statements, so this form of statement can only be used in <c:choose>,
<c:when> and <c:otherwise> tags together to complete.
8. C:fortokens> Label
Description
Items that are looped are not
The delims separator is no
Begin Condition No 0
End end Condition No the last item in the collection
Step Step No 1
var represents the current item's variable name no no
Varstatus shows the loop state of the variable No
Example:
<c:fortokens items= "A:b:c:d" delims= ":" var= "token" >
<c:out value= ""/>
</c:forTokens>
The use of this tag is equivalent to the Java.util.StringTokenizer class. Here the string a:b:c:d to: Loop four times apart,
Token is a string that loops to the current split.
9. <c:redirect> Label
Description: The label redirects the request to another page that has the following property properties that describe whether a default value must be
URL URL address is None
Context/followed by the name of the local Web application no current application
Example:
<c:redirect/' >http://www.yourname.com/login.jsp '/>
Redirect the request to the Http://www.yourname.com/login.jsp page, which is equivalent to Response.setredirect
("http://www.yourname.com/login.jsp");
10. <c:param> Label
Description:<c:param> tags are used to pass parameters to a redirect or include a page, which has the following property properties that describe whether
The default value must be
Name the variable name set in the request parameter is no
Value of the variable set in the request parameter no no
Example:
<c:redirect url= "login.jsp" >
<c:param name= "id" value= "888"/>
</c:redirect>
Pass the parameter 888 to the LOGIN.JSP page with the ID name, which is equivalent to login.jsp?id=888
11. <fmt:> Formatting Tags
Description: Need to import <%@ taglib prefix= "FMT" http://java.sun.com/jsp/jstl/fmt ">http://java.sun.com/jsp/jstl/fmt"%>
1) Format date <fmt:formatdate value= "" pattern= "Yyyy-mm-dd HH:mm:ss"/>
Value: The date value taken by an El expression or <%new date ()%>;
Pattern: The date format of the output;

2) format Digital <fmt:formatnumber
Value= "${n}" pattern= "###,###.##"/>

El expression (Detailed)

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.