EI语法


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

EI是jsp-2.0规范的一部分,tomcat-5.x版本以上都已经能够支持jsp-2.0规范,
<%@ page isELIgnored=”true” %> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言。

.语法结构(1)使用el的时候,默认会以一定顺序搜索四个作用域,将最先找到的变量值显示出来。

??? ${expression}

1,与页面元素的操作:
??

如果我们有${username}这样一个正则表达式,它回去依次调用pageContext.getAttribute(“username”) -> request.getAttribute(“username”) -> session.getAttribute(“username”) -> application.getAttribute(“username”),只要找到某一个不为空的值就立刻返回。

?(2)指定具体的域

??????

el中的作用域 对应关系
pageContext(1) 当前页的pageContext对象
pageScope 把page作用域中的数据映射为一个map对象
requestScope(2) 把request作用域中的数据映射为一个map对象
sessionScope 把session作用域中的数据映射为一个map对象
applicationScope 把application作用域中的数据映射为一个map对象
param 对应request.getParameter()
paramValues(3) 对应request.getParameterValues()
header(4) 对应request.getHeader()
headerValues 对应request.getHeaderValues()
cookie(5) 对应request.getCookies()
initParam(6) 对应ServletContext.getInitParamter()

?? session.getAttribute(“username”)?? 等同于 ${sessionScope:username}

?? request.getParameter(String name)? 等同于 ${param:name}

(3)EI运算符

???? ${ 1 + 2 }? 结果:3

???? ${ 1 <= 2 } 结果: true

??? ${ true && false } 结果:false

??? ${ empty 2 } 结果:false //判断对象是否为空

??? ${ not empty 2 }结果:true

?? ${ 2>1 ? “yes” : “no” }

? 对string类型进行操作:

? ${fn:substring(booking.orderId,0,6)}

?? fn:contains(string, substring)? fn:endsWith(string, suffix)?? fn:length(item)? fn:split(string, separator)? fn:trim(string)

(4)调用对象方法

<c:forEach var=”modules” items=”${SES_powers.modules}” varStatus=”status”>
?????
? </c:forEach>

SES_powers.modules 会自动调用SES_powers的getModules()方法,(即使没有modules这个变量),这样避免了在页面中引用JAVA代码

注:向方法中传参http://tech.ddvip.com/2009-04/1238857014113535.html

一般最好在程序中就将数据取出,在JSP页面只进行读取数据的操作

(5)集合操作

Map
Map mapValue? = new HashMap();
?? mapValue.put(“key1”, “value1”);
?? mapValue.put(“key2”, “value2”);

request.setAttribute(“mapvalue”, mapValue);
———————————————El表达式获取
<li>输出map,采用.进行导航,也称存取器</li><br>
mapvalue.key1:${mapvalue.key1 }<br>
mapvalue.key2:${mapvalue.key2 }<br>

?List

${userlist[4].username }

(6)数组

? User[] users = new User[10];

${users[2].username }