1、表达式输出属性
先来看一个简单的表达式小例子
el.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% request.setAttribute("attribute", "request"); %> ${attribute} </body> </html>
访问el.jsp,页面上会显示request。
${attributeName}就可以输出属性对应的值。
我们再来看一个例子
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("attribute", "page"); request.setAttribute("attribute", "request"); session.setAttribute("attribute", "session"); application.setAttribute("attribute", "application"); %> ${attribute} <!--类似 out.print(${attribute}所代表的内容) --> </body> </html>
现在设置了四个属性名都是"attribute",只是他们的范围不同。
这时候又会又会输出什么呢?
为什么是page呢?
因为表达式根据属性名获取属性值时,是根据pageScope->resquestScope->applicationScope这样的顺序进行查找的。
如果所有Scope内都没有要寻找的属性,则页面为空白(即输出“ ”空白字符串)。
2.表达式输出指定范围属性
${attributeName}是按照指定的顺序查找,那么我们有没有办法访问指定范围的属性呢?
当然可以,这就要用到
${pageScope.attributeName} 访问page范围attributeName的值
${requestScope.attributeName} 访问request范围attributeName的值
${sessionScope.attributeName} 访问session范围attributeName的值
${applicationScope.attributeName} 访问application范围attributeName的值
el.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("attribute", "page"); request.setAttribute("attribute", "request"); session.setAttribute("attribute", "session"); application.setAttribute("attribute", "application"); %> ${pageScope.attribute}<br> ${requestScope.attribute}<br> ${sessionScope.attribute}<br> ${applicationScope.attribute}<br> </body> </html>
3、表达数输出对象属性、集合
表达式不仅可以获取属性的值,而且也可以获取对象集合的值。
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <!-- 表达式输出对象属性 --> <% Person user = new Person("zhansan","man",20); session.setAttribute("onLineUser",user); %> <!-- onLineUser就想当于 user对象, ${onLineUser.name}相当于out.print(user.name) --> ${onLineUser.name } ${onLineUser.sex } ${onLineUser.age }<br> ${onLineUser}<br> <!-- 表达式输出List --> <% List<String> list = new LinkedList<String>(); for(int i = 0; i < 3; i++){ list.add("周" + i); } session.setAttribute("list", list); %> ${list[0]} ${list[1]} ${list[2]}<br><br> <!-- 表达式输出Map --> <% Map<Long,Person> mapL = new HashMap<Long,Person>(); //Long和person mapL.put(new Long(1), new Person("KeyIsLong" + 1, "man", 20)); session.setAttribute("mapL", mapL); Map<Integer,Person> mapI = new HashMap<Integer, Person>();//Integer和person mapI.put(new Integer(2),new Person("KeyIsInteger"+2, "woman", 20)); session.setAttribute("mapI", mapI); Map<String,String> mapStr = new HashMap<String,String>();//String和person mapStr.put("1", "KeyIsString"); session.setAttribute("mapStr", mapStr); %> <!-- 表达式通过键(long类型)获取值--> ${mapL[1].name} ${mapL[1].sex} ${map[1].age}<br> <!-- 此处map[1]就代表一个Person对象 --> <!-- 表达式通过键(int类型)获取值--> ${mapI[2].name} ${mapI[2].sex} ${mapI[2].age}<br> <!-- 表达式通过键(String类型)获取值--> ${mapStr["1"]}<br> </body> </html>
我们会发现“KeyIsInteger”这个没有被打印出来,这是应为EL表达式在解析Integer类型数字的时候,会自动把数字转换成Long类型,
后台使用Integer类型作为key值,进行判断的时候Integer与Long对象不相等。导致无法取出key值。将key换成Long就可以取出了。
就相当于放入是 map.put(new Integer(x), xxx); ,但表达式取出时是map.get(new Long(x));
4、表达式的运算符操作
运算操作(+,-,*,/,%):
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table border="2"> <tr> <td>2+3 = ${2+3}</td> <td>2+3.0 = ${2 + 3.0}</td> <td> 2.0+3.0 = ${2.0+3.0}</td> </tr> <tr> <td>2-3 = ${2-3}</td> <td>2.0-3 = ${2.0-3} </td> <td> 2.0-3.0 = ${2.0-3.0}</td> </tr> <tr> <td>2*3 = ${2*3}</td> <td>2.0*3 = ${2.0 * 3}</td> <td> 2.0*3.0 = ${2.0*3.0}</td> </tr> <tr> <td>2/3 = ${2/3}</td> <td>6 / 2 = ${6 / 2} </td> <td> 6.0/1.0 = ${6.0 / 1.0 }</td> </tr> <tr> <td>2/3 = ${2%3}</td> <td>6 / 2.0 = ${6 % 2.0} </td> <td> 6.0%1.0 = ${6.0 / 1.0 }</td> </tr> </table> </body> </html>
5、表达式条件运算符(> 、< 、>=、 <=、 ==、 !=):
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table border="2"> <tr> <td>2.0>2.0 ${2.0>3.0}</td> <td>2>3.0 ${2 > 3.0}</td> <td> 2.0>3.0 ${2.0>3.0}</td> </tr> <tr> <td>2.0<2.0 ${2.0<2.0}</td> <td>2.0<3 ${2.0<3} </td> <td> 2.0<3.0 ${2.0<3.0}</td> </tr> <tr> <td>2>=2.0 ${2>=2.0}</td> <td>2.0>=2.0 ${2.0 >= 2.0}</td> <td> 2.0>=2 ${2.0>=2}</td> </tr> <tr> <td>2.0<=2.0 ${2.0<=2.0}</td> <td>2.0 <= 2 ${2.0 <= 2} </td> <td> 6.0<=1.0 ${6.0 <= 1.0 }</td> </tr> <tr> <td>2==2 ${2==2}</td> <td>2.0== 2.0 ${2.0== 2.0} </td> <td> 2.0==2 ${2.0== 2 }</td> </tr> <tr> <td>2!=2 ${2!=2}</td> <td>2.0!= 2.0 ${2.0!= 2.0} </td> <td> 2.0!=2 ${2.0!= 2 }</td> </tr> </table> </body> </html>
6、表达式逻辑运算符(&&、|| )及三目运算符
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% session.setAttribute("one", 1); session.setAttribute("two", 2); %> <table border="2"> <tr> <td>true && 3>2 :${true && 3>2 }</td> <td>one < two && true :${one < two && true} </td> <td>one < two && false :${ one < two && false}</td> </tr> <tr> <td>true || 3>2 :${true || 3>2 }</td> <td>one < two || true :${one < two || true} </td> <td>one < two || false :${ one < two || false}</td> </tr> </table> <!-- 三目运算符 --> ${one < two ? "1小于2" : "2小于1"} </body> </html>
7、表达式获取cookie:${cookie} 代表一个存放所有Cookie的map对象。map<xxx,Cookie>
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <c:forEach items = "${cookie}" var = "co"> <!-- 将Cookie所有键值对输出 --> ${co.key } :${co.value} :${co.value.value} <br> <!-- co.key代表key:JSESSIONID,co.value代表对应的Cookie对象 --> ${cookie.JSESSIONID.value} <!-- cookie.JSESSIONID代表map中key为JSESSIONID的Cookie对象,类似map.getKey(JSESSIONID).value --> <!-- cookie.JSESSIONID.value代表Cookie对象的值 --> </c:forEach> </body> </html>
8、表达式获取请求参数(param):
param代表存放所有请求参数name和value的map对象。
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> name:${param.name} sex:${param.sex} age:${param.age} <br><!-- 通过key获取value,类似map.getKey(xxx) --> <c:forEach items="${param}" var = "p"> <!--迭代输出map所有key value--> ${p.key}:${p.value} </c:forEach> </body> </html>
http://localhost:8080/JSP/el.jsp?name=hcf&sex=man&age=20
9、表达式获取一个请求参数所包含的多个值(paramValues)
paramValues:代表所有参数名和值的map对象,map<xxx,String[]>
值部分是一个字符串数组,所以可以储存多个参数。可用于多选按钮中获取值。
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${paramValues.name[0]} ${paramValues.name[1]}<br> <!-- key对应多个value --> ${paramValues.age[0]} <!-- key对应单个value --> </body> </html>
http://localhost:8080/JSP/el.jsp?name=hcf&name=zrx&age=20
10、其他运算符:
empty 判空
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% session.setAttribute("zhansan","zhansan"); session.setAttribute("lisi",null); %> zhansanIsEmpty:${empty zhansan}<br> <!-- 不是空返回false --> lisiIsEmpty:${empty lisi}<br> <!-- 是空返回true --> wangwuIsEmpty:${empty wangwu}<br> </body> </html
11.自定义表达式函数
用户可以自定义表达式函数,调用的函数必须为java类的静态方法,调用方式${prefix:method(params)}
自定义表达式函数主要步骤
1.编写java类及其静态方法。
2. .tld文件中配置方法
3.JSP页面导入方法并使用
Java类及静态方法:自定义EL表达式方法,实际上调用的是Java类中的一个静态方法。
public class ElMethod { public static String elOut(String msg) {//表达式方法必须为静态 return "elMethod" + msg; } }
创建.tdl文件并配置。配置定义的静态方法。
elMethod.tld
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <short-name>EL Function</short-name> <uri>/elMethod</uri> <!-- 设置uri地址 --> <!-- 描述一个EL自定义函数 --> <function> <description>html标签转义处理方法</description> <!-- name用于指定自定义函数的名称--> <name>elOut</name> <!-- 名称 --> <!-- 指定调用方法所在类 --> <function-class>com.myclass.ElMethod</function-class> <!-- 返回值类型 函数名(参数类型1,参数类型2, ...) --> <!-- 设置返回值类型和参数类型 --> <function-signature>java.lang.String elOut(java.lang.String)</function-signature> </function> </taglib>
el.jsp
<%@ page language="java" import = "java.util.*,com.myclass.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri = "/elMethod" prefix = "el" %> <!-- 引入自定义方法 --> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${el:elOut("456")} <!-- 调用自定义方法 --> </body> </html>
参考资料:
https://www.cnblogs.com/xdp-gacl/p/3938361.html
https://stackoverflow.com/questions/924451/el-access-a-map-value-by-integer-key