EL (Expression Language) and Jstl tags (JSP standard tag Library)

Source: Internet
Author: User
Tags null null

One, El Expressions: Expression language provides the ability to use run-time expressions outside of the scope of the JSP scripting element (for example, script tags). Scripting elements are elements in a page that can be used to embed Java code in a JSP file. They are typically used for object manipulation and for performing calculations that affect what is generated

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]}, 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.

It will be searched sequentially from page, Request, Session, application range (from small to large).

If it is found to stop, and does not display NULL, but does not display the value, which is the advantage of the El expression, but also the use of the El expression to judge the value should be noted

For example: Use the S tag in JS and match the El expression value to judge

var username = ${username};

<s:if test= "username ==null" > Such a judgment is wrong, because the El expression does not show null if it does not go out of value

The correct way is: <s:if test= ' username = = "' > (note the use of single and double quotes) or: <s:if test=" ${username ==null} ">

The name of the attribute range in El is used: equivalent to:

Page pagescope ${Pagescope.page_name}

Request requestscope ${Requestscope.request_name} <%=request.getattribute ("Request_ Name ");%>

Session sessionscope ${Sessionscope.session_name} <%=session.getattribute ("Sessio N_name ");%>

Application Applicationscope ${Applicationscope.application_name} <%=application.getattribute ("Applic Ation_name ");%>

Second, effective expression

Valid expressions can contain literals, operators, variables (object references), and function calls.

1) Text:

Value of literal text

Boolean true and False for example: ${true}

The Integer is similar to Java. Can contain any positive or negative numbers, such as 24,-45, 567 for example: ${23+45}

Floating point is similar to Java. Can contain any positive or negative floating-point numbers, such as -1.8E-45, 4.567

String any strings that are qualified by single or double quotation marks. For single quotes, double quotes, and backslashes, use a backslash character as an escape sequence.

It is important to note that single quotes do not need to be escaped if double quotes are used at both ends of the string.

NULL NULL

2) operator

The JSP expression language provides the following operators, most of which are common operators in Java:

Term definitions

Arithmetic type +,-(two Yuan), *,/, Div,%, mod,-(unary)

Logical AND, &&, or, | | 、!、 not

Relationship type = =, EQ,! =, NE, GT, <=, le, >=, GE. Can be compared with other values, or with Boolean, String, Integer, or floating-point literals.

An empty operator is a prefix operation that can be used to determine whether a value is empty. Example: ${empty user.username} ${not empty User.username}

Conditional type A? B:c. Assigns a value B or C according to the result of a assignment.

3) An Implicit object

The most commonly used implicit objects are ${param} and ${paramvalues}.

param maps the request parameter name to a single string parameter value (obtained by calling Servletrequest.getparameter (string name).

The GetParameter (String) method returns a parameter with a specific name. The expression $ (param. Name) is equivalent to <%=request. GetParameter ("username")% >

For example, a parameter passed in a page's address bar can be removed from another page in this way

Paramvalues maps the request parameter name to a numeric array (obtained by calling Servletrequest.getparameter (String name).

It is very similar to the Param implicit object, but it retrieves an array of strings instead of a single value. The expression ${paramvalues. Name) is equivalent to Request.getparamtervalues (name).

The header stores the data that the user's browser and server use to communicate (by calling Servletrequest.getheader (String name).

Example: To get the version of the user's browser, you can use ${header["User-agent"}. The expression ${header. Name} is equivalent to Request.getheader (name).

Headervalues It is possible that the same header name has different values (obtained by calling Servletrequest.getheaders (String)).

It is very similar to an implicit object. The expression ${headervalues. Name} is equivalent to Request.getheadervalues (name).

The cookie maps the cookie name to a single Cookie object. A client request to the server can obtain one or more cookies.

The expression ${cookie. Name. Value} returns the first cookie value with a specific name.

If the request contains more than one cookie with the same name, you should use the ${headervalues. Name} expression.

Initparam maps the context initialization parameter name to a single value (obtained by calling Servletcontext.getinitparameter (String name).

Expression ${initparam.userid} is equivalent to Application.getinitparameter ("userid");

PageContext obtain additional information about user requirements or pages.

${pagecontext.request.querystring} The parameter string that gets the request is equivalent to <%=request.getquerystring ()%>

${pagecontext.request.requesturl} Gets the requested URL, but does not include the requested parameter string <%=request.getrequesturl ()%>

${pagecontext.request.contextpath} The name of the Web application of the service is equivalent to <%=request.getcontextpath ()%>

${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

In addition: The Pagescope, Requestscope, Sessionscope and Applicationscope mentioned above are also an implicit object of El

For example: <%=user.getaddr ()%> is equivalent to ${USER.ADDR} (user is a variable, stored in action or Severlet in the specified scope, addr as user's property)

III. Special emphasis on

1. Note that when an expression references one of these objects by name, the corresponding object is returned instead of the corresponding property.

For example, if an existing PageContext property contains some other value, ${pagecontext} also returns the PageContext object.

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

Iv. JSTL (C) Tags:

Because the EL expression cannot iterate through the value of the collection object, it is possible to resolve the problem by using the method in the JSTL tag to remove the collection object

Jstl is a language based on El expression, which belongs to a standard tag outside the JSP, and the Library import label format is: <%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

The Jstl section describes:

1, <c:out value= "${property name}" ></C:OUT>: Output label

2. Iteration Label

Syntax: <c:foreach items= "userlist" var= "user" varstatus= "status" begin= "int" end= "int" step= "int" >

Loop body

<c:out value= "${status.index}" ></c:out>

<c:out value= "${status.count}" ></c:out>

<c:out value= "${user.name}" ></c:out>

<c:out value= "${user.age}" ></c:out>

</c:forEach>

Description: 1) Items: is a collection, with an El expression;

2) Var: variable name, storing items, representing each piece of data in the collection

3) Varstatus: A variable that shows the loop state, with a few properties:

①index: starting from 0; Displays the index value of the current iteration

②count: Element position, starting from 1; Displays the line position displayed by the current iteration, by matching the statement, to achieve odd, even row of different colors for partitioning

③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;

3, <c:if test= "judgment statement" ></C:IF>: if the condition in test is satisfied, then the statement between the <c:if></if> tag pair is executed, which is equivalent to the If .... The structure function

4, <c:choose>: Because Jstl is not shaped as if () {...} else {...} Conditional statements, so this form of statement can only be used in <c:choose>

Structural form:

<c:choose>

<c:when test= "judgment statement 1" > EXECUTE statement 1</c:when>

<c:when test= "judgment Statement 2" > EXECUTE statement 1</c:when>

..........

<c:otherwise> do not match before, execute this default statement

</c:otherwise>

</C:CHOOSE>: Used to select a statement that satisfies a condition between pairs of labels, equivalent to a switch statement

5., <c:redirect> label

Description: The label redirects the request to another page that has the following properties

Example: <c:redirect url= "http://www.baidu.com/login.jsp"/>

Redirect the request to the Http://www.baiddu.com/login.jsp page, equivalent to Response.setredirect ("http://www.yourname.com/login.jsp");

6. <c:param> Label

Description The:<c:param> tag is used to pass parameters to a redirect or include page, which has the following properties

Example: <c:redirect url= "http://www.baidu.com/login.jsp" >

<c:param name= "paramname" value= "Paramvalue"/>

</c:redirect>

This is displayed in the Address bar in the form: Http://www.baidu.com/login.jsp?paramName=paramValue

V. <fmt:> format the label and display

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; litigation

For example:

<fmt:formatdate value= "${date}" pattern= "Yyyy-mm-dd"/>

<fmt:formatdate value= "${date}" pattern= "yyyy year mm DD Day"/>

2) format digital <fmt:formatnumber value= "${number}" pattern= "###,###.##"/>

EL (Expression Language) and Jstl tags (JSP standard tag Library)

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.