Java journey (iii) --- JSTL and EL expressions
Zookeeper
Let's take a look at the JSP code to see how it feels?
<% List
usELList = pageModel.getList(); for (ItELator
itEL=usELList.itELator(); itEL.hasNext();) { UsEL usEL = itEL.next();%>
<%=usEL.getUsELId() %> <%=usEL.getUsELName() %> <%=usEL.getContactTel()%> <%=usEL.getEmail() %> <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(usEL.getCreateDate()) %> <% } %>
This is the JSP page mixed with Java code. Its shortcomings are obvious:
(1) increased difficulty in jsp maintenance;
(2) The error prompt is unclear and cannot be easily debugged;
(3) The division of labor is unclear, and the artist also needs to understand the Java code;
The above three points will eventually increase the development cost of the program;
JSTL and EL expressions are used to solve the above problem. JSTL allows JSP developers to reduce the need for script elements, or even eliminate the need for them to reduce code workload and simplify the interface, it is easy to maintain. The background java staff can work with front-end artists to improve the efficiency of team development.
So what is JSTL? What is an EL expression? Why do we need to talk about EL expressions every time we talk about JSTL? What is the relationship between them? What is the difference? Let's take a look at the following ~
1. What is JSTL?
JSTL is a well-developed open-source JSP tag library. JSTL1.0 consists of four custom tag libraries (core, format, xml, and SQL) and a pair of universal tag library validators.
(1) The core tag Library provides custom operations to manage data by limiting the scope of variables and performing iteration and conditional operations on page content. It also provides tags for generating and operating URLs;
(2) The format tag library defines operations for formatting data (especially numbers and dates;
(3) The xml library contains some tags that are used to manipulate data expressed in XML;
(4) The SQL database defines the operations used to query relational databases.
2. What is an EL expression?
EL expressions are mainly used to find data in the scope and perform simple operations on them. They are not programming languages, or even scripting languages. It is usually used together with the JSTL mark and can express complex behaviors with simple and convenient symbols.
EL expression format: it is bounded by the dollar sign ($) and the content is included in curly brackets. Example: $ {pagination. UserName }. I will not go into details about this again. There are many extremely detailed tutorials on the Internet.
Iii. Scope of EL expression variables?
EL implicit objects: pagination, requestScope, sessionScope, and applicationScope. The scope ranges from small to large.
EL name/JSTL name |
Valid range |
Pagination/Page |
User request this page Process |
RequestScope/Request |
User's entire request process |
SessionScope/Session |
User's entire session |
ApplicationScope/Application |
WEB application execution period |
When $ {username} does not specify the scope of the variable, it searches for the variable in the above order by default and returns the result if it is found. If no range is found, null is returned. If the scope is specified, the search process is as follows:
Instance |
Description |
$ {Pagination. UserName} |
Obtain UserName |
$ {RequestScope. UserName} |
Get the UserName in the Request range |
$ {SessionScope. UserName} |
Get the UserName in the Session range |
$ {ApplicationScope. UserName} |
Obtain the UserName in the Application range. |
4. What are the differences between JSTL and EL expressions?
(1) function: EL is used to display data. Its function is the same as <% = expression %>, but its function is limited, for example: default non-empty string output is provided for null, and iteration loop array elements cannot be implemented. JSTL makes up for the above problems. It is often used to implement logic judgment and iterative display of web pages, with the same functions as <% program code %>. Therefore, in general, we will use JSTL with EL expressions, so that Java code is not displayed in JSP.
(2) configuration: Both JSTL and EL expressions are custom tags. The web server container parses jsp into a servlet and calls the parser During the parsing process, this set of parsers includes the EL, JSTL, and other parsers.
But EL is a servlet standard, and containers are integrated. Therefore, when using EL expressions, we do not need to reference anything. JSTL needs to introduce its package for use. Use the taglib command to indicate the tag library used by the JSP page. <% @ taglib uri = "" prefix = "%>
(3) NULL value processing: when no data is found in all search ranges, NULL is returned, but the EL expression is optimized. The page is blank, rather than printing the output NULL. While JSTL is optimized more thoroughly. When NULL is returned, JSTL can return "NONE ".
Now, I have a basic understanding of JSTL and EL expressions. To put it bluntly, I just want to combine them to completely eliminate the Java code of JSP pages!