jsp的el表达式

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> //记得导jstl这个包

 

 

<% request.setAttribute("name","lili"); %>

使用EL表达式获取数据:${name} <!-- 页面输出结果:lili -->

<hr>

 

<%

Person p = new Person();

p.setAge(12);

request.setAttribute("person",p);

%>

使用el去表达式获取数据 ${person.age} <!-- 页面输出结果:12 -->

 

<!-- 在jsp页面中,使用el表达式获取list集合中指定位置的数据 -->

<%

Person p1 = new Person();

p1.setName("孤傲苍狼");

p1.setAge(100);

Person p2 = new Person();

p2.setName("白虎神皇");

List<Person> list2 = new ArrayList<Person>();

list2.add(p1);

list2.add(p2);

 

//将集合放在内置对象中,

request.setAttribute("list",list2);

%>

${list[0].name} <!-- 页面输出结果 ,孤傲苍狼 -->

${list[1].name} <!-- 页面输出结果 ,白虎神堂 -->

<!-- 迭代List集合 ,items-->

<c:forEach var="abc" items="${list}">

${abc.name} <!-- 网页输出结果 孤傲苍狼,白虎神堂->

${abc.age} <!-- 网页输出结果,100,0-->

</c:forEach>

<hr>

 

<!-- 在jsp页面中,使用el表达式获取map集合的数据 -->

<%

Map<String,String> map = new LinkedHashMap<String,String>();

map.put("a","aaaaxxx");

map.put("b","bbbb");

map.put("c","c键对应的值");

map.put("1","1键对应的值");

request.setAttribute("map",map);

%>

<!-- 根据关键字取map集合的数据 -->

${map.c} <!-- 网页输出结果:c键对应的值 -->

${map["1"]} <!-- 网页输出结果:1键对应的值 -->

<!-- 迭代Map集合 -->

<c:forEach var="me" items="${map}">

${me.key}=${me.value}<br/>

</c:forEach>

<!-- 网页输出结果-->

<!-- a=aaaaxxx ,b=bbbb,c=c键对应的值,1=1键对应的值 -->

原文地址:https://www.cnblogs.com/binghuaZhang/p/10780877.html