这里主要是要表达el表达式怎么写能取到值,对循环集合没有过多的代码表达
<%
Student student = new Student("陈毛毛",18,"上海");
request.setAttribute("s1", student);
%>
${s1}
<!-- 想 取出 student 的 其他的 信息: 姓名, 年龄, 城市 -->
<!-- 在 el 表达式 中 都是 调用 了 其 getXXX方法 -->
${s1.name }
${s1.age }
${s1.city }
上面为什么只有一个${s1}呢?是要说明在循环里面表示要循环集合时,那个集合应该的写法。如果单独这样写在页面,是不行的。
<%
String[] names = {"西门庆","西门崔雪","西门抽血","西门抽筋"};
request.setAttribute("names", names);
%>
${names}
${names[0] }
${names[1] }
${names[2] }
${names[3] }
解释同上,看${names}和上面${s1}一个道理
<!-- 取出 list 集合中的 数据 -->
<%
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list", list);
%>
${list}
${list[0] } <!-- list.get(O) -->
${list[1] } <!-- list.get(1) -->
${list[2] }
<!-- 取出 map 中的 数据 -->
<%
Map map = new HashMap();
map.put("aaa.ddd", "111");
map.put("bbb", "222");
map.put("ccc", "333");
request.setAttribute("map", map);
%>
${map } <!-- { key=value,key=value} -->
${map["bbb"] }
${map["ccc"] }
${map["aaa.ddd"] }
上面程序段通过运行能取到值
下面注意数组号
<span style="background-color: rgb(240, 240, 240);"><%</span> <span style="white-space:pre"> </span>List list1 = new ArrayList();
list1.add(new Student("李寒",18,"上海"));
list1.add(new Student("孙在驰",19,"香港"));
list1.add(new Student("李羿成",17,"东京"));
request.setAttribute("student", list1);
%>
${student[0].name }
${student[0].age }
${student[0].city }