Detailed instructions on the use of EL Expression Language and jstl tag library in JSP pages, eljstl
Limitations of JavaBean in JSP
To obtain the JavaBean attribute, you must instantiate it.
Forced type conversion
<% Employee employee = (Employee)request.getAttribute("employee"); Computer comp = employee.getComputer(); String manufacturer = comp.getManufacturer();%>
If a logic error occurs in page compilation, code execution errors may occur and errors may occur during page compilation on the JSP page.
Solution
Simplified using EL expressions
What is EL?
EL is Expression Language)
EL features
Replaces the complex code in the JSP page
EL syntax
$ {EL exprission}
$ {Bean. name} or $ {bean ['name']}
$ {Bean. name} is actually calling the getName () method of bean.
Note: 1 el accesses data through. And. For example, $ {applicationScope. user. userName} or $ {applicationScope. user ["userName"]}
2. If the accessed property name contains some special characters, such as "." or "-" and other non-letter or digit symbols, you must use []
3 applicationScope. user ["user-Name"]
4. If variables are used for input, you can only use []. For example:
<% String data = "username"; %>
$ {ApplicationScope. user [data]}
5 of course, there are four implicit objects: applicationScope, sessionScope, requestScope, pagscope. correspond to application, session, request, and pageContext. For example, applicationScope is used to obtain the attribute values in the application range.
Automatic Conversion Type
The data type can be automatically converted when EL obtains data.
More relaxed restrictions on Types
Easy to use
Compared with embedding Java code in JSP, EL application is simpler
EL syntax
Start with "$ {" and end "}"
Get the value directly using the variable name $
$ {Username}
Variable attribute range name
Name in attribute range EL
Page pagination, for example, $ {pagination. username}, indicates to search for the username variable in the page range. If no value is found, Null is returned.
Request requstScope
Session sessionScope
Application applicationScope
EL Operator
Operator "[]"
Operator "."
Function
Get Object Attributes
Obtain data in an object set
Saves a list set of users in the Session.
Simple Application: Output name display
<%
Map names = new HashMap ();
Names. put ("one", "LiYang ");
Names. put ("two", "WangHua ");
Request. setAttribute ("names", names );
%>
Name: $ {names. one} <br/>
Name: $ {names ["two"]} <br/>
Introduction to EL implicit objects
Object Name Description
Pagination returns the variable names in the page range, which have been mapped to the corresponding values.
RequestScope returns the variable names in the request range. These names have been mapped to the corresponding values.
SessionScope returns the variable names in the session range. These names have been mapped to the corresponding values.
ApplicationScope returns the variables within the application range and maps the variable names to the corresponding values.
Param returns the string value of the client's request parameter
ParamValues returns a set of values mapped to the client's request parameters.
PageContext provides access to user requests and page information
The EL expression can simplify JSP page code, but what if logical judgment is required?
Click to view JSTL label library Introduction