EL
Expression Language, expressions language, through the operation exists in the PageContext and so on data, the implementation of JSP to write more simple, simply use El without introducing jar package, as long as the container support.
The hidden object of El
El Basic use
${LD} is equivalent to looking for the LD variable sequentially from Pagescope-requestscope-sessionscope-applicationscope, returning its value if there is one, without returning NULL if the variable position is determined, and can be obtained directly, For example, ${requestscope.ld}. In addition to the implied objects mentioned above, El considers all other content as a variable in a domain.
El syntax
In addition, El can also perform common operations
- Arithmetic operators + 、-、 *,/(or DIV) and% (or mod)
- relational operators = = (or eq),! = (or NE), < (or LT), > (or GT), <= (or Le), and >= (or GE)
- logical operators && (OR and), | | (OR) and! (or not)
- Validation operator Empty
El Get attribute parsing
Assuming that you have already added the user object to Requestscope, now you want to get the Name property of the User object: Use ${user.name} to get the Name property of the user object, the way to obtain the property is not through the variable that is defined private String Username, instead of removing get and lowercase for name based on Method GetName (), modify the username to username1 use of ${user.name} without effect.
El Get Complex properties
Use El to get the Name property of the group property of the User object, you can use ${user.group.name}, get the array with El the second parameter can be ${array[1]}, get K1 in the map can be obtained for ${MAP.K1}.
The JSTL is usually used in conjunction with El.
Jstl
JSP standard tag library,jsp, is a continuous improvement of the open source JSP tag library, its use requires container support, can be elegant label alternative to the Java code in JSP. Jstl can provide more controllable details than El.
Configure Usage Configuration
Use Jstl to introduce Jstl.jar and Standard.jar two packages, add tags to jsp files
- <%@ taglib uri="Http://java.sun.com/jsp/jstl/core" prefix="C"%>
Core
The core library provides common tags for out, set, remove, and foreach, using examples:
- <c:choose >
- <c:when test="${v1 GT v2}" >
- V1 Greater than V2
- </c:when>
- <c:otherwise>
- V1 Greater than V2
- </c:otherwise>
- </c:choose>
Format
The format library provides control labels for formatting, such as:
- Today (default):<fmt:formatdate value="${today}"/><br/>
- Today (default):<fmt:formatdate value="${today}" type="date"/>< br/>
- Today (default):<fmt:formatdate value="${today}" type="Time"/><BR />
- Today (default):<fmt:formatdate value="${today}" type="both"/>< br/>
- Today (default):<fmt:formatdate value="${today}" type="Both" datestyle="full" /><br/>
- Today (default):<fmt:formatdate value="${today}" pattern="Yyyy/mm/dd HH:mm:ss" /><br/>
Can get:
Summarize
In general, through Jstl and El, can reduce the JSP page programming work, in the way of labeling optimization, through the function of Jstl and El Display, make JSP responsibility more single, development division more clear.
About Jstl and El