首页 > 代码库 > EL的基本使用

EL的基本使用

 

技术分享

技术分享

总结:EL操作的是作用域

<body>
<%
Users users = new Users("lisi","lisi123","lisi@lisi.com");
Address address = new Address("四川","成都","武侯区");
users.setAddress(address);
Users users1 = new Users("lisi1","lisi123","lisi@lisi.com");
Address address1 = new Address("四川1","成都1","武侯区1");
users1.setAddress(address1);
Users users2 = new Users("lisi2","lisi123","lisi@lisi.com");
Address address2 = new Address("四川2","成都2","武侯区2");
users2.setAddress(address2);
Users users3 = new Users("lisi3","lisi123","lisi@lisi.com");
Address address3 = new Address("四川3","成都3","武侯区3");
users3.setAddress(address3);

String prop = "city";

pageContext.setAttribute("u", users);
request.setAttribute("u", users1);
//session.setAttribute("u", users2);
application.setAttribute("u", users3);
%>
<%=((Users)request.getAttribute("u")).getUsername() %>
<%=((Users)request.getAttribute("u")).getAddress().getProvince() %>
<hr>
<!--
如果对应的作用域中没有相应的属性的时候 直接返回空字符串


默认取值的规则 按照作用于的范围从小到大的去查找
-->
${name}
${u.username }<br>
${u.address.province }

${u.address[‘province‘] }

${u.address[prop] }
<!--
如果去那的属性在对应的对象中没有 则会报错
-->
<hr>
<%-- ${u.address[‘prop‘] } --%>

<!--
关于el表达式的作用域

如果不知道作用域的时候 默认从小到大 从PageContext(page)开始查找
如果要指定作用域 由各大作用域的别名+Scope指定 ${requestScope.u}

-->
${sessionScope.u.username }

<hr>

${u[param.name] }<br>
test:${u.test1 }
<!--
el表达式 不能嵌套
-->
<%-- ${u[${param.name}] } --%>
</body>

 

testcookie.jsp

<body>
<%

session.setAttribute("name", "lisi");
%>
${cookie.JSESSIONID.maxAge }
</body>

testparam.jsp

<body>
<!--
param包装了 从前端传进来的数据 通过键名去得到对应的值
如果有同名不同值的额情况 获取第一个

如果要获取同名的所有值 用paramValues去得到 返回一个字符串的数组 当我们输入的下标越界了 会返回空字符串

-->
Param:${param.name }
Params:${paramValues.name[2] }
</body>

el操作集合:

<body>
<%
List<String> list = new ArrayList<String>();

Map<String,Object> map = new HashMap<String,Object>();

for(int i=0;i<20;i++){
list.add("test list "+(i+1));
map.put("a"+(i+1),"test map "+(i+1));
}
request.setAttribute("l", list);
request.setAttribute("m", map);

%>
${l[1] }
<hr>
${m.a5 }<br>
${m[‘a6‘] }
</body>

EL的基本使用