Javaweb review of the nineth El expression

Source: Internet
Author: User
Tags define function tld

Introduction: The development of El expression is very much, now let's review about if you want to manipulate El expressions

1:el expression Syntax

All El expressions are terminated by {start}, and expressions are used with the. and [] operator to access the data analogy ${user.username}, ${user["UserName"]} is equivalent.

2: Several important operators

Arithmetic, logic, and relational operators are the same as our Java syntax operations.

2.1:empty operator

The empty operation is to determine whether a value is null or "", such as ${!empty user}

2.2: Three mesh operator

The EL expression also supports the three-mesh operator ${a? result 1: Result 2} For example: ${1+1>2?1:2} The result of the output is 2

3: Hidden Objects

There are 11 hidden objects in the El expression, and I'll enumerate them.

3.1:pagecontext

PageContext is an instance of Javax.servlet.jsp.PageContext that can access objects such as servletcontext,request,response,session through PageContext.

3.2:pagescope

We have seen the scope of JSP before, and this is the same as the JSP scope, from the meaning we can see that this is mainly to get the page range of properties. Let's look at an example and we'll start by creating a JSP file

<%UserBean User1=new  UserBean (); user1.setage(N); User1.setemail ("[Email Protected]User1.setusername ("John Doe");p Agecontext.setattribute ("user1", user1); %>

Then we started to visit ${pagescope.user1.username} and I also dropped the first PageContext app with ${pagecontext.request.requesturi}

<body>${pagecontext.request.requesturi}${pagescope.user1.username}</body>

3.3:requestscope

Primarily get the value of the property within the request scope, modify the code in the above to the following code

<%UserBean User1=new  UserBean (); user1.setage(N); User1.setemail ("[Email Protected]User1.setusername ("John Doe"); Request.setattribute ("user", user1) ; %>

The El in JSP is ${requestscope.user1.username}

3.4:sessionscope

This primarily gets the value of the properties in the session scope. Let's start by creating a servlet class and assigning a value to the session

protected void throws servletexception, IOException {                UserBean user=new  UserBean ();        User.setage (+);        User.setemail ("[email protected]");        User.setusername ("Zhang San");        HttpSession session=request.getsession ();        Session.setattribute ("user", user);    }

Then in el.jsp to get the value of the session property ${sessionscope.user.username}, remember to run the Servlet class first and then run the el.jsp effect as follows

3.5:applicationscope

This is the value that gets the object in the global scope, which is the same as the say and.

3.6:param

If we want to get the parameter value of the URL (http://localhost:8080/taglib-test/el.jsp?userName= Zhao four) request, we can use ${param.username} to get

3.7:paramvalues

The key to get the parameter request parameter information is an array, with not many

3.8:header

The header information used to get the request, such as ${header["User-agent"} to get the value of the parameter in the header information

3.9:headervalues

Used primarily to obtain the value of the request header information.

3.10:cookie

Needless to say too much

3.11:initparam

Gets the value of the Web application initialization parameter primarily

4: Named variable

In the El expression, the value of the named variable is found through the Pagecontext.findattribute () method, the analogy above if we do not write their scope, then will follow Page,request,session, Application to find, so we try to write, so as to achieve the purpose of optimizing the program.

5:el Expressions using Functions

This is like a custom tag, and we can also use a function

First we define a class (I create a very simple)

 Public class Elfun {    publicstatic  string Gethelloworld (String str)    {        return  str;    }}

Then we are creating a TLD file

<?xml version= "1.0" encoding= "GBK"?> <taglib xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "version= "2.0" > <!--define function versions--<tlib-version>1.0</tlib-version> <!--define function names-- > < Short-name>el</ Short-name> <!--define the first function--<function> <!--define a function--<name>hello&lt ;/name> <!--defining function processing class-<function-class>com.lp.els.elfun</function-class> <!--the corresponding method of defining functions---<function-signature>java.lang.String Gethelloworld (java.lang.String)</function-signature> </function> </taglib>

One thing to note: This is the definition of the function name that you want the EL expression in your JSP to refer to. Don't be mistaken.

Then we can reference it on the JSP page

<body><%@ taglib uri= "/web-inf/tlds/el.tld" prefix= "el"%>${el:hello ("Hello World")} </body>

From here we see that the hello behind El is not gethelloworld in our class. There is no habitual in the El expression after the addition of the number, will bring an error.

Javaweb review of the nineth 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.