JSP expression language

Source: Internet
Author: User
Tags arithmetic operators

first, 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 variable whose name is username in a range.
Because we do not specify a range of username, it will be searched sequentially from page, Request, Session, application range.
If the way to find username, direct callback, no longer continue to find, but if all the scope is not found, the return null.
Name of the attribute range in El
Page Pagescope
Request Requestscope
Session Sessionscope
Application Applicationscope

Two, El hidden objects
1. Implied objects related to the scope
The El implied objects associated with the 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, which is getattribute (String name), but cannot obtain other related 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. Implied 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

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.

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

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

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

three, El operator
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)}

Four, 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

---------------------------------------------

Add:

<%@ taglib prefix= "C" uri= "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&GT;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 "!".
---------------------------------------------

Add:

1 El expressions are represented in ${} and can be used in all HTML and JSP tags instead of complex Java code in JSP pages.

2 El expressions 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 values that return a request parameter. pagescope represents a variable for a page range. Requestscope represents a variable for the Request object. Sessionscope represents a variable in the session scope. Applicationscope represents the variable that is scoped.

3 <%@ page iselignored= "True"%> indicates whether the El language is disabled, and true to suppress. False indicates that the. JSP2.0 enabled El language is not suppressed by default.

4 El language can display a logical expression such as ${true and false} result is a false relationship expression such as ${5>6} result is false arithmetic expression such as ${5+5} result is 10

5 The variable search scope in El is: page request session application dot operator (.) and "[]" are the values that represent getting variables. The difference is [] A variable that can display non-speech

JSP expression language

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.