The JSP EL expression
EL expression
You can use JSP EL (Expression Language) expressions. EL expression is "$ {}" script enclosed, the object to be more easily read. JSP EL expression is equivalent to the "<% =%>" script can not be written in the "<%>" scripts.
EL expression field data acquisition
Simple data acquisition
${pageScope|requestScope|sessionScope|applicationScope.属性名}
note:
1. To get to the acquisition, get less returns ""
2. Get quick way to get value by $ {property} domain name:
order from pageScope | requestScope | sessionScope | Find applicationScope specified property
if found, return immediately and the end of the second look
if no return ""
3. If there is an attribute names "+" or "_" or "." when other special symbols, in this way: $ {xxxScope [ "attribute name" ]}
Obtaining complex data
${arrayObject[index]}<%--获取数组中的数据--%>
${listObject[index]}<%--获取list中的数据--%>
${mapObject.key}<%--获取map中的数据--%>
${beanObject.attribute}<%--获取javabean中的数据--%>
The operation is performed
- EL expression supports some simple arithmetic operations, such as: plus (+), subtract (-), multiply (*), divide (/ or div), take the remainder (or the% mod) and the like.
- EL expression supports some simple relational operators, such as: greater than (> or gt), less than (<or lt), is equal to (== or EQ), ranging from (or = NE!), Greater than or equal (≥ or ge) , less than or equal (≤ or Le), is empty (empty).
- EL expression supports some simple logic operations, for example: and (&& or and), or (|| or or), and non-brackets (or not!).
- EL expressions also supports conditional operator (:?).
Note: When comparing character, EL expression calls int compare (char ss) to compare methods. Equal operation, EL expression calls equals () methods were compared.
Hidden Objects
pageScope | Four domain objects, scope of the object will be mapped to a map object |
requestScope | |
sessionScope | |
applicationScope | |
param | Save all represent a Map object request parameters |
paramValues | Save all represent a Map object request parameters, it is for a request parameter, returns a string [] |
header | Map object represents a saving of all http request header fields |
headerValuse | Represents a saving all http Map object request header fields, for which a request parameter, returns a string [] |
Initfrm | It represents a saving of all web application initialization parameters of map objects |
cookie | Map object represents a saving of all the cookie |
pageContext | JSP page corresponds to pageContext object $ {pageContext.request.contextPath}: dynamically acquired road project jsp pages |
The sample code
el.jsp
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="bean.Product" %>
<%--
Created by IntelliJ IDEA.
Author: XJM
Date: 2019-02-25
Time: 20:21
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el</title>
</head>
<body>
<%
String str = "el";
request.setAttribute("str", str);
String strs[] = new String[]{"first", "second", "third"};
request.setAttribute("strs", strs);
List list = new ArrayList();
list.add("list data1");
list.add("list data2");
list.add("list data3");
request.setAttribute("list", list);
Map<String, Integer> map = new HashMap();
map.put("year", 1998);
map.put("month", 12);
map.put("day", 26);
request.setAttribute("map", map);
Product product = new Product();//Product类拥有4个属性,并设置了相应的get与set方法
product.setId(1);
product.setPdesc("mi 9 骁龙855");
product.setPname("mi 9");
product.setPrice(2999.0);
request.setAttribute("product", product);
int i = 7;
request.setAttribute("i", i);
char ch = '9';
request.setAttribute("ch", ch);
%>
<h1>el表达式</h1>
老方法:<%=request.getAttribute("str")%><br/>
el表达式:${str}<hr/>
老方法:<%=request.getAttribute("strs")%><br/>
el表达式:${strs}<hr/>
老方法:<%=((String[])request.getAttribute("strs"))[1]%><br/>
el表达式:${strs[1]}<hr/>
老方法:<%=request.getAttribute("list")%><br/>
el表达式:${list}<hr/>
老方法:<%=((List)request.getAttribute("list")).get(2)%><br/>
el表达式:${list[2]}<br/><%--el表达式中,list有两种获取方法,可以根据索引获取--%>
${list.get(2)}<hr/><%--也可以通过get方法获取--%>
老方法:<%=request.getAttribute("map")%><br/>
el表达式:${map}<hr/>
老方法:<%=((Map)request.getAttribute("map")).get("year")%>-<%=((Map)request.getAttribute("map")).get("month")%>-<%=((Map)request.getAttribute("map")).get("day")%><br/>
el表达式:${map.year}-${map.month}-${map.day}<hr/><%--el表达式中map中的值可以省略get,直接通过key获取--%>
老方法:<%=((Product)request.getAttribute("product")).getId()%>-<%=((Product)request.getAttribute("product")).getPname()%>-<%=((Product)request.getAttribute("product")).getPrice()%>-<%=((Product)request.getAttribute("product")).getPdesc()%><br/>
el表达式:${product.id}-${product.pname}-${product.price}-${product.pdesc}<hr/><hr/>
<h1>el运算</h1>
<h2>算术运算</h2>
i + ch = ${i + ch}<br/><el表达式中int值与数字类型的char值进行算术运算,会可以转化为数字进行运算>
5 - 4 = ${5 - 4}<br/>
29 / 3 = ${29 div 3}<br/>
31 % 29 = ${31 % 29}<hr/>
<h2>关系运算</h2>
i < ch ${i < ch}<br/>
map为空 ${empty map}<br/>
100.0 = 100 ${100.0 eq 100}<hr/>
<h2>逻辑运算</h2>
7 != 5 || 4 == 4 ${7 != 5 || 4 == 4}<br/>
1 != 2 && 7 ${1 != 2 && 7}<br/>
!(1 == 3) ${not (1 eq 3)}<hr/>
<h2>条件运算</h2>
10 > 5 ? 10 - 5 : 10 < 5 ${10 gt 5 ? 10 - 5 : 10 lt 5}<hr/>
</body>
</html>
operation result