[Java] JSP notes-EL, JSTL common tags

Source: Internet
Author: User

First, what is the EL language

The expression language (EL) is a simple language introduced by JSP 2.0 to compute and output Java objects.

Ii. the role of the EL language

To make the JSP easier to write. The expression language is inspired by the ECMAScript and XPath expression languages, which provide a way to simplify expressions in the JSP. It is a simple language based on the available namespaces (PageContext property), nested properties, and accessors for collections, operators (arithmetic, relational, and logical), extensible functions that map to static methods in Java classes, and a set of implicit objects .

Third, the use of the EL language before the configuration

In Tomcat5.0.16 and later and in version, the default is to support El, we do not need to make any other configuration.

Iv. How to use EL expression 1. El's Built-in objects

First we need to know what the EL's built-in objects are----pagescope, Requestscope, Sessionscope, Applicationscope, and if scope is not specified, the default from Pagescope to Applicationscope once to expand the scope to find the property name, you can also use the Xxxscope. Property name directly specified in a scope lookup, such as:

${requestscope.tom}  
2. Grammatical structure
${expression}
3. [] with the. operator

EL provides. And [] Two operators to access data. Such as:

    ${student.name}      ${studentlist[0].name}  

When the name of the property to be accessed contains some special characters, such as. or? Other than letters or numbers, be sure to use "[]". Such as:

    ${student. My-name}   <!--${student. My-name} is not spelled correctly and should be changed to the following---      ${student[  "My-name"}

If you want to dynamically fetch values, you can do it with "[]" and "." cannot be dynamically evaluated. Such as:

${Sessionscope.student[property]}    <!--where property is a variable that dynamically takes the properties of an object, such as "id", "name", and so on.  
4. Use EL to remove data from built-in objects (1), normal objects, and object properties

Server-side:

Request.setattribute ("Student", student);  

Print the server-side bound data on the browser:

    ${Student}  <!--equivalent to executing the student.tostring ();--      ${  student.name}<!--is equivalent to executing the Student.getname (); --      ${student.teacher.name  }<!--equivalent to executing the Student.getteacher (). GetName ();  
(2), the data in the array

Server-side:

    New string[]{"Tom", "Lucy", "Lilei"};      Request.setattribute ("NameArray", NameArray);             New Student[3];      students[0] = stu1;      students[1] = stu2;      students[2] = stu3;      Request.setattribute ("Students", students);  

Print out the server-side bound array data on the browser:

    ${Namearray[0]}   <!--Tom--      ${namearray[1]}   <!--Lucy--and      ${namearray[
    2]}   <!--lilei--            ${students[0].id}   <!--output First Student's ID--      ${Students [1].name}   <!--output The second student's name--      ${students[2].teacher.name}   <!-- Output the name of the third student's teacher--  
(3), the data in the list

Server-side:

    list<student> studentlist=New arraylist<student>();      Studentlist.add (STU1);      Studentlist.add (STU2);      Studentlist.add (STU3);      Request.setattribute ("Studentlist", studentlist);  

Print the list data for the server-side bindings on the browser:

    ${Studentlist[0].id}   <!--output First Student's ID--      ${studentlist[1].name}   <!--output The name of the second student-- >      ${studentlist[2].teacher.name}   <!--output The name of the third student's teacher--  
(4), data in map

Server-side:

    New Hashmap<string, student>();      Studentmap.put ("Tom", STU1);      Studentmap.put ("Lucy", STU2);      Studentmap.put ("Lilei", STU3);      Request.setattribute ("Studentmap", Studentmap);  

Print the server-side binding map data on the browser:

    ${StudentMap.Tom.id}   <!--output First Student's ID--      ${studentMap.Lucy.name}   <!--output The name of the second student- -      ${studentMap.Lilei.teacher.name}   <!--output The name of the third student's teacher--  
(5), using EL to remove data from an implicit object

The JSP expression language defines a set of implicit objects, many of which are available in JSP Scriplet and expressions. The following implicit objects can be used:

PageContent The context of the JSP page. It can be used to access JSP implicit objects such as requests, responses, sessions, outputs, ServletContext, and so on. For example, ${pagecontext.response} assigns a value to a page's response object.

In addition, several implicit objects are available that allow easy access to the following objects:

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. An expression of $ (param. Name) is equivalent to Request.getparameter (name).
Paramvalues Map 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).
Header Map the request header name to a single string header value (obtained by calling Servletrequest.getheader (string name). The expression ${header. Name} is equivalent to Request.getheader (name).
Headervalues Map the request header name to a numeric array (obtained by calling Servletrequest.getheaders (String)). It is very similar to an implicit object. The expression ${headervalues. Name} is equivalent to Request.getheadervalues (name).
Cookies 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 The context initialization parameter name is mapped to a single value (obtained by calling Servletcontext.getinitparameter (String name).

In addition to the above two types of implicit objects, there are also objects that allow access to a wide range of variables, such as Web contexts, sessions, requests, and pages:

Pagescope Maps the variable name of a page range to its value. For example, an EL expression can use ${pagescope.objectname} to access a page-scoped object in a JSP, and you can use ${pagescope. ObjectName. AttributeName} to access the properties of an object.
Requestscope Map the variable name of the request scope to its value. The object allows access to the properties of the requested object. For example, an EL expression can use ${requestscope. ObjectName} to access an object of a JSP request scope, and you can use ${requestscope. ObjectName. AttributeName} to access the properties of the object.
Sessionscope Maps a session-scoped variable name to its value. The object allows access to the properties of the session object. For example:
${sessionscope. Name}
Applicationscope Maps application-scoped variable names to their values. The implicit object allows access to application-scoped objects.

V. EL operator

Arithmetic type +,-(two Yuan), *,/, Div,%, mod,-(one yuan)
Logic type 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.
Empty Empty An empty operator is a prefix operation that can be used to determine whether a value is empty.
Conditional type A? B:c. Assigns a value B or C according to the result of a assignment.

[Java] JSP notes-EL, JSTL common tags

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.