EL表达式
EL表达式语言,简称EL
目的:为了使JSP页面编写起来更简单
EL表达式的语法格式:${标识符}
它必须以“${“开始,以”}”结束 具体表达式的内容
${studentName+1}——结果为1
studentName:作用域对象中设置的属性,JSP引擎调用了pageContext.findAttribute(studentName),会在4个作用域范围内去找名字叫studentName的属性,如果找到,返回该属性的值并和1做加法,如果没有找到,返回空串,空串和1做加法会自动类型转换
JSP EL用于以下情形
--静态文本:
eg:<body>${studentName+1}</body>
--标准标签或自定义标签(可以作为元素属性的值,也可以在自定义或者标准动作元素的内容中的使用):
<body bgcolor=”${pageScope.color}”>
--EL不能在脚本元素中使用
一个EL表达式包含变量、文字常量、操作符。
文字常量主要包括字符串、数字和布尔值、还有null
<br/>${true}<%--布尔类型常量 --%>
<br/>${10}
<br/>${10.5f}
<br/>${“hello”}
<br/>${null}<%--不会输出null,用空串去替代了 --%>
操作符主要有:算数运算符、关系运算符、逻辑运算符、验证运算符empty、条件运算符
算数运算符主要有平时常用的“+”、“-”、“*”、“/”
5+3=${5+3}
关系运算符主要有“==”、“!=”、“<”、“>”、“<=”、“>=”
<br/>5<3 ${5<3}
<br/>5>3 ${5>3}
逻辑运算符主要有“&&”、“||”、“!” 。
<br/>true&& false ${true && false}
验证运算符“empty”与条件运算符“?”
验证运算符:empty作为前缀,用来检索一个值是否为null或者empty.如${emptyuser.name}用来判断user对象中的name的值是否为null
条件运算符:${条件?truevalues:falsevalue},如果条件为真,则表达式的值为truevalue,否则为falsevalue
EL表达式的隐式对象
在EL表达式中总有11个隐式对象
与作用范围有关的EL隐式对象包含:
pageScope requestScope sessionScope applicationScope
它们可以读取使用JSP内置对象pageContext、request、session以及application的setAttribute()方法所设定的对象的数值-----即getAttribute(String name),却不能取得其他相关信息。
注意:如果没有设定使用EL内置对象的作用范围,则按照pageScope、requestScope、sessionScope和applicationScope的先后顺序读取属性值。
与输入有关的隐式对象:
param和paramValues,它们是EL中比较特别的隐含对象
要取得用户的请求参数时
– request.getParameter(String name)
– request.getParameterValues(String name)
在EL中则可以使用param和paramValues两者来取得数据:
– ${param.name}
– ${paramValues.name}
其他隐式对象:
cookie
– 用来取得使用者的cookie值,例如在cookie中设定了username属性值,可以使用${cookie.username.value}来取得属性值。
header和headerValues
– 读取请求的头数据,使用header或headerValues内置对象,例如${header[“User-Agent”]},headerValues则用来取得所有的头信息,等价于调用request.getHeaders()方法。
initParam
– initParam用来读取设置在web.xml中的参数值。例如${initParam.repeat},等价于:(String)application.getInitParameter(“repeat”); 或
servletContext.getInitParameter(“repeat”);
pageContext
– pageContext用于取得其他有关用户要求或页面的详细信息
pageContext应用:
${pageContext.request.queryString}取得请求的参数字符串
${pageContext.request.requestURL} 取得请求的URL,不包括参数字符串
${pageContext.request.contextPath} 服务的web application 的名称
${pageContext.request.method} 取得HTTP 的方法(GET、POST)
${pageContext.request.protocol} 取得使用的协议(HTTP/1.1、HTTP/1.0)
${pageContext.request.remoteUser} 取得用户名称
${pageContext.request.remoteAddr} 取得用户的IP 地址
${pageContext.session.new} 判断session 是否为新的
${pageContext.session.id} 取得session 的ID
${pageContext.servletContext.serverInfo} 取得主机端的服务信息
禁掉EL表达式方法(page指令的优先级高):
1、<%@pageisELIgnored="true|false"%>
true:表示忽略对EL表达式进行计算。
false:表示计算EL表达式。
– isELIgnored默认为false。
2、
把整个web应用程序中的EL表达式都禁掉
在web.xml文件中配置
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
如何用EL来获取类、集合和Hashtable中的值
<%--模拟Servlet获取数据封装在StudentBean对象中,把该对象存入到某个作用域 对象中--%>
<%
StudentBean st=new StudentBean();
st.setId(1);
st.setName("lisi");
request.setAttribute("student",st);
%>
<%--请求转交,如果用${student["id"]}这种形式,如果id是表达常量必须加引号
,不加引号代表变量。如果属性名中包含特殊字符时,必须用中括号的方法
动态取值也要用中括号的--%>
${student.name}
${student["id"]}
<br/>
<%
String userName="ding";
request.setAttribute("user",userName);
%>
${user}<%--pageContext.findAtribute("user") 顺序的从pageContext、request、sesion、application--%>
<br/>${true}<%--布尔类型常量 --%>
<br/>${10}
<br/>${null}<%--不会输出null,用空串去替代了 --%>
<br/>
5+3=${5+3}
<br/>5<3 ${5<3}
<br/>5>3 ${5>3}
<br/>true &&false ${true && false }
<%--\${5+4} 原样输出,用转义字符\,\只对它后面的一句有效 --%>
<br/>\${5+4}${5+4}
<br/>${empty user}
<%--javabean对象中某些属性又是另一类的对象 --%>
<br/><%--模拟Servlet --%>
<%
Addres address=new Addres();
address.setPro("河北省");
address.setCity("保定市");
StudentBean stu=new StudentBean();
stu.setId(2);
stu.setName("TOM");
stu.setAddress(address);
request.setAttribute("student1",stu);
%>
${student1.id}<br/>
${student1.name}<br/>
${student1.address.pro}
${student1.address.city}<br/>
<%--集合对象的输出 --%>
<%
List list=new ArrayList();
list.add(0,newStudentBean(3,"额安格"));
list.add(1,newStudentBean(4,"网哦"));
list.add(2,newStudentBean(5,"王杰"));
list.add(3,new StudentBean(6,"网络"));
request.setAttribute("studentList",list);
%>
${studentList[1].id} ${studentList[1].name}<br/>
<%--Map类型的集合 --%>
<%
HashMap hm=new HashMap();
hm.put("aaa",newStudentBean(10,"aaaaa"));
hm.put("bbb",newStudentBean(20,"bbbbb"));
hm.put("ccc",newStudentBean(30,"ccccc"));
hm.put("111",newStudentBean(40,"ddddd"));
request.setAttribute("studentMap",hm);
%>
${studentMap.aaa.id} ${studentMap.aaa.name}<br/>
<%--用EL表达式取数据时通常用点,当用点去不出来时,用中括号 --%>
${studentMap["111"].id}<br/>
<%--隐式对象 --%>
<%
session.setAttribute("website","ww.csdn.com");
%>
${sessionScope.website}<br/>
<%--读取在请求对象中封装的参数 --%>
${param.pageNo}
<br/>${paramValues}
<br/>
<%
Cookie cookie=new Cookie("name","zhangsan");
cookie.setMaxAge(24*3600);
response.addCookie(cookie);
%>
${cookie.name.value}
<br/>${headerValues}
<br/>${header["user-agent"]}
<%--等价于application.getInitParameter() --%>
${initParam.encoding}
<br/><a href="${pageContext.request.contextPath}/login.html">下一页</a>