Java Web Review Article 9 EL expressions and Java Web Article 9 el
EL expression is widely used in development. Now let's review how to operate EL expressions.
1: EL expression syntax
All EL expressions end with {start. and the [] operator to access the data metaphor $ {user. userName} and $ {user ["userName"]} are 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 used to determine whether a value is null or "", as shown in $ {! Empty user}
2.2: Three-object Operator
The EL expression also supports the three-object operator $ {? Result 1: result 2}, for example, ${1 + 1> 2? } The output result is 2
3: Implicit object
In EL expressions, there are 11 hidden objects. I will list them one by one.
3.1: pageContext
PageContext is an instance of javax. servlet. jsp. PageContext. objects such as ServletContext, Request, Response, and Session can be accessed through pageContext.
3.2: pagination
We have seen the scope of Jsp before. This is the same as the scope of Jsp. From the meaning, we can see that this is mainly used to obtain the attributes within the scope of the page. Let's look at an example. We will first create a jsp file.
<% UserBean user1 = new UserBean (); user1.setAge (12); user1.setEmail ("123@outlook.com"); user1.setUserName (""); pageContext. setAttribute ("user1", user1); %>
Then we started to access $ {pagination. user1.userName}. I also added $ {pageContext. request. requestURI} to the first pageContext application}
<body>${pageContext.request.requestURI}${pageScope.user1.userName}</body>
3.3: requestScope
Obtains the attribute values within the request range. The code above is changed to the following code:
<% UserBean user1 = new UserBean (); user1.setAge (12); user1.setEmail ("123@outlook.com"); user1.setUserName (""); request. setAttribute ("user", user1); %>
El in jsp is $ {requestScope. user1.userName}
3.4: sessionScope
This mainly obtains the attribute values in the session range. Create a Servlet class and assign a value to the Session.
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {UserBean user = new UserBean (); user. setAge (19); user. setEmail ("16455@qq.com"); user. setUserName ("Zhang San"); HttpSession session = request. getSession (); session. setAttribute ("user", user );}
Then get the Session attribute value $ {sessionScope. user. userName} in el. jsp. Remember to run the Servlet class and then run el. jsp as follows:
3.5: applicationScope
This is the value of the object in the global range. This is much the same as above.
3.6: param
If we want to obtain the URL (http: // localhost: 8080/taglib-test/el. jsp? UserName = Zhao Si) The request parameter value can be obtained using $ {param. userName }.
3.7: paramValues
This is mainly used to obtain the information related to the parameter request parameters.
3.8: header
Used to obtain the request header information, such as $ {header ["User-Agent"]} to obtain the parameter values in the header information.
3.9: headerValues
It is mainly used to obtain the value of the request header information.
3.10: cookie
Needless to say, too many
3.11: initParam
Obtains the initialization parameters of web applications.
4: naming Variables
In EL expressions, the named variable value is pageContext. the findAttribute () method is used to find the completed data. If we do not write their range, we will search for the data by page, Request, Session, and application, so we try to write the data as much as possible, in this way, the program can be optimized.
5: EL expressions use functions
This is similar to a custom tag. We can also use functions.
First, define a class (I created a very simple one)
public class ElFun { public static String getHelloWorld(String str) { return str; }}
Then we create 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 the function version --> <tlib-version> 1.0 </tlib-version> <! -- Define the function name --> <short-name> el </short-name> <! -- Define the first function --> <function> <! -- Define a function --> <name> hello </name> <! -- Fixed function processing class --> <function-class> com. lp. els. ElFun </function-class> <! -- Define the corresponding method of the function --> <function-signature> java. lang. string getHelloWorld (java. lang. string) </function-signature> </function> </taglib>
Note: The Defined Function Name is the el expression in your jsp to be referenced.
Then we can reference
<body><%@ taglib uri="/WEB-INF/tlds/el.tld" prefix="el"%>${el:hello("Hello World")}</body>
Here we can see that hello after el is not getHelloWorld in the class. Also, do not habitually Add a number after the el expression, which may lead to errors.