百度百科中解释EL(Expression Language)表达式,就一句话:为了使JSP写起来更加简单。
这就是我们程序员梦寐以求的,El表达式让我们的jsp页面更加简洁。
*************************开启学习EL表达式之路*************************************************
EL表达式在哪里使用,是做什么的?
- 在jsp页面中使用,专门来做输出的。
- 受三大指令之一的page指令控制,如:<%@page isELIgnored=”true”%>表示页面限制使用EL表达式;页面不写,默认为不限制。
- 如果希望忽略某个EL表达式,可以在EL表达式之前添加“\”,例如:\${4 + 5},其结果不输出。
jsp页面中有那么多标签,为什么还使用EL表达式?
- jsp页面中可以使用的标签有:
- <% … %>:自定义标签,里面自定义java代码
- <%! … %>:压根儿就不该使用,徒增烦恼,忘记吧
- <%= … %>:输出标签,等同于<% out.println(...) %>
- EL表达式以一种面向对象的思想很好的和java思想融合了,使用EL表达式,方便输出,使页面能够让非java人员看懂,便于美工等人员编辑。
EL表达式语法
- EL使用方式:${...}
- EL输出的值为null时,则不输出
- EL的四个域对象,它们都是Map类型
- pageScope
- requestScope
- sessionScope
- applicationScope
- EL运算符
-
运算符 说明 范例 结果 + 加 ${17+5} 22 - 减 ${17-5} 12 * 乘 ${17*5} 85 /或div 除 ${17/5}或${17 div 5} 3 %或mod 取余 ${17%5}或${17 mod 5} 2 ==或eq 等于 ${5==5}或${5 eq 5} true !=或ne 不等于 ${5!=5}或${5 ne 5} false <或lt 小于 ${3<5}或${3 lt 5} true >或gt 大于 ${3>5}或${3 gt 5} false <=或le 小于等于 ${3<=5}或${3 le 5} true >=或ge 大于等于 ${3>=5}或${3 ge 5} false &&或and 并且 ${true&&false}或${true and false} false !或not 非 ${!true}或${not true} false ||或or 或者 ${true||false}或${true or false} true empty 是否为空 ${empty “”} true
注:empty,可以判断字符串、数组、集合的长度是否为0,为0返回true。empty还可以与not或!一起使用。${not empty “”}
- 举例:
- 1.在jsp中对四大内置对象赋值,使用EL表达式输出
<body> <% //给四大内置对象赋值 pageContext.setAttribute("a", "pageContext"); request.setAttribute("a", "request"); session.setAttribute("a", "session"); application.setAttribute("a", "application"); %> <!-- 使用EL表达式将各个域中的值输出,只能是输出 --> ${pageScope.a }<br/> ${requestScope.a }<br/> ${sessionScope.a }<br/> ${applicationScope.a }<br/> <!--使用param进行全局查找 --> ${a } </body>
- 1.在jsp中对四大内置对象赋值,使用EL表达式输出
-
页面打印为:
pageContext request session application pageContext
2.进阶,在Servlet中像三大域赋值,在jsp页面中使用EL表达式输出
package com.yaxin; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Mode01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); System.out.println("Mode01.java"); request.setAttribute("aaa", "request域"); request.getSession().setAttribute("aaa", "session域"); request.getServletContext().setAttribute("aaa", "application域"); request.getRequestDispatcher("/index3.jsp") .forward(request, response); return; } }
index3.jsp
<body> ${requestScope.aaa }<br/> ${sessionScope.aaa }<br/> ${applicationScope.aaa }<br/> ========<br> ${aaa } </body>
页面打印为:request域 session域 application域 ======== request域
3.使用EL表达式运算符,参与运算,可直接得出结果${1+2 }
- EL表达式格式
- 操作List和数组:${list[0]}、${arr[0]};
-
操作bean的属性:${person.name}、${person[‘name’]},对应person.getName()方法;
-
操作Map的值:${map.key}、${map[‘key’]},对应map.get(key)。
- 操作List和数组:${list[0]}、${arr[0]};
EL内置对象
EL一共11个内置对象,无需创建即可以使用。这11个内置对象中有10个是Map类型的,最后一个是pageContext对象。
- pageScope
- requestScope
- sessionScope
- applicationScope
以上四个内置对象,对应四大域!它们都是Map<String,Object>类型!
使用EL操作Map:${map.key}以及${map[‘key’]}
- param:Map<String,String>,它对应参数!!!;
Ø 对应request.getParameterMap();
Ø 操作:${param.username},等同与request.getParameterMap().get(“username”);
- paramValues:Map<String,String[]>;
Ø 对应request.getParameterMap();
Ø 操作:${param.username},它是一个String[]
- header:Map<String,String>,对应请求头;
Ø header[‘User-Agent’]等同与request.getHeader(“User-Agent”);
- headerValues:Map<String,String[]>;
Ø header[‘User-Agent’][0]等同与request.getHeaders(“User-Agender”),再遍历第一个元素
- initParam:Map<String,Object>,对应的是<context-param>中的初始化参数!;
Ø 它对应项目的初始化参数
Ø ${initParam.cxt_xxx}等同与ServletContext#getInitParameter(“cxt_xxx”)。
- cookie:Map<String,Cookie>,如果要获取一个cookie值:${cookie.xxx.value};
Ø key:cookie的名称
Ø value:cookie对象
Ø ${cookie.JSESSIONID.value}
- pageContext:PageContext类型
域相关内置对象--重要
四个域内置对象
- pageScope:${pageScope.name}等同于pageContext.getAttribute("name");
- requestScope:${requestScope.name}等同于request.getAttribute("name");
- sessionScope:${sessionScope.name}等同于session.getAttribute("name");
- applicationScope:${applicationScope.name}等同于application.getAttribute("name");
如果在域中保存的是JavaBean对象,那么可以使用EL来访问JavaBean属性。因为EL只做读取操作,
所以JavaBean一定要提供get方法,而对set方法没有要求。
Student.java
package com.bean; public class Student { private String name; private int age; private int classes; public Student(String name, int age, int classes) { super(); this.name = name; this.age = age; this.classes = classes; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getClasses() { return classes; } public void setClasses(int classes) { this.classes = classes; } }
index4.jsp
<body> <% pageContext.setAttribute("student", new Student("zhangsan",18,3)); %> 使用EL表达式的域内置对象获取bean对象属性值:<br/> 姓名:${pageScope.student.name }<br/> 年龄: ${pageScope.student.age }<br/> 班级: ${pageScope.student.classes }<br/> </body>
全域查找:${student}表示依次在pageScope、requesScopet、sessionScope、appliationScope四个域中查找名字为student的属性。
请求参数相关内置对象
param和paramValues这两个内置对象是用来获取请求参数的。
1.param:Map<String,String>类型,param对象可以用来获取参数,与request.getParameter()方法相同。
<body> 使用EL内置对象:param对象获取请求参数:<br> EL:${param.username }<br/> EL:${param.password }<br/> ========================== 使用java片段获取请求参数:<br/> <% String username = request.getParameter("username"); String password = request.getParameter("password"); out.println(username+"--"+password); %> </body>
注意,在使用EL获取参数时,如果参数不存在,返回的是空字符串,而不是null。这一点与使用request.getParameter()方法是不同的。
2.paramValues:paramValues是Map<String, String[]>类型,当一个参数名,对应多个参数值时可以使用它。
应用初始化参数相关内置对象
initParam:initParam是Map<String,String>类型。它对应web.xml文件中的<context-param>参数。
Cookie相关内置对象
cookie:cookie是Map<String,Cookie>类型,其中key是Cookie的名字,而值是Cookie对象本身。
先在访问setIndex.jsp页面,再访问getIndex.jsp页面。
pageContext对象
pageContext:pageContext是PageContext类型!可以使用pageContext对象调用getXXX()方法.
例如pageContext.getRequest(),可以${pageContext.request}。也就是读取JavaBean属性.
EL表达式
说明
${pageContext.request.queryString}
pageContext.getRequest().getQueryString();
${pageContext.request.requestURL}
pageContext.getRequest().getRequestURL();
${pageContext.request.contextPath}
pageContext.getRequest().getContextPath();
${pageContext.request.method}
pageContext.getRequest().getMethod();
${pageContext.request.protocol}
pageContext.getRequest().getProtocol();
${pageContext.request.remoteUser}
pageContext.getRequest().getRemoteUser();
${pageContext.request.remoteAddr}
pageContext.getRequest().getRemoteAddr();
${pageContext.session.new}
pageContext.getSession().isNew();
${pageContext.session.id}
pageContext.getSession().getId();
${pageContext.servletContext.serverInfo}
pageContext.getServletContext().getServerInfo();
**************************************************完**********************************************