Java El Expression

Source: Internet
Author: User
Tags arithmetic operators

El expression

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 variable whose name is username in a range. 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, 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

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 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 within a session scope. Applicationscope represents a variable that applies scope.

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} The result is a false relationship expression such as ${5>6} result is false arithmetic expression such as ${5+5} result is 10

The variable search scope in 5--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

2. El Hidden Objects

1) Implied objects related to the range

The El implied objects that are related to a range include the following four: Pagescope, Requestscope, Sessionscope, and Applicationscope, which are basically the PageContext, request, and The session is the same as the application; in El, these four hidden objects can only be used to obtain the range attribute value, that 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) 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: A generic 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, which is the output of the Username property value of the Logininfo object to the current position of the page. ${...} is the syntax of expression Language (EL) in JSP2.0. It defines an expression in which the expression can be a constant (as above) or a concrete expression (such as in the case of the Foreach Loop body). Typical cases are as follows:

? ${logininfo.username}

This indicates that the Username property of the Logininfo object is referenced. We can pass the "." The ${logininfo[username operator refers to the properties of an object, and you can also use "[]" to refer to an object property, such as "/"} and ${logininfo.username} to achieve the same effect. The meaning of the "[]" reference is that if a special character appears in the property name, such as "." Or "-", you must use "[]" to get the value of the property to avoid grammatical conflicts (this phenomenon should be avoided as much as possible during system development). 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, and for an expression such as ${logininfo.username}, the first thing to look for is whether the variable logininfo was defined before the current page. If not found, go to the request, Session, and application range until you find it. Returns null if no matching variable is found until the end. If we need to specify the scope of 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 returned as attribute in the HttpServletRequest object (see the spring source for specific implementations

Implementation code for the Org.springframework.web.servlet.view.InternalResourceView.exposeModelAsRequestAttributes method), which means that spring MVC , the resulting 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 achieve a similar purpose by <c:choose>tag:

<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. Where the <c:when> clause resembles a case clause, it can occur more than once. The above code outputs the "*" number on an odd line, and an even row outputs "!".

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 conditions (that is, the values of user.generous and user.stingy are not true) show all user.generous and User.stingy are false.

Because Jstl is not shaped as if () {...} else {...} Conditional statements, so this form of statement can only be done with <c:choose>, <c:when> and <c:otherwise> tags.

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= "###,###.##"/>

Java El Expression

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.