先引用两个实体bean
package com.EL.bean; public class person { private String username; private String password; private Address address; public person() { } public person(String username) { this.username = username; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.EL.bean; public class Address { private String city; public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
//--EL表达式是为了取值的,取出servlet域中(request,session,application)中的数据<br> //${取值 }相当于pageContent.findAttribute("取值")<br> //取值通过.或者[] <% // (servlet中产生数据,于普通方式转发到jsp),把数据存到域中。那么jsp取出servlet中的数据? String data="jack"; request.setAttribute("data",data); %> ${data} <br> <% //(servlet产生数据,将数据封装为javabean),在带给jsp页面,那么jsp页面怎么取值呢? person p=new person(); p.setUsername("lucy"); request.setAttribute("person",p); %> ${person.username} //相当于person类有个Getusername()方法 <% //(servlet 复杂bean,将数据封装为复杂javabean),在调用另一个javabean,jsp怎么取值呢? person p1=new person(); Address a=new Address(); a.setCity("nanjing"); p1.setAddress(a); request.setAttribute("person",p1); %> <br>${person.address.city} <% //servlet中产生数据,如多个人的集合对象list,那么jsp怎么取值呢 List list=new ArrayList(); list.add(new person("aaa")); list.add(new person("bbb")); list.add(new person("ccc")); list.add(new person("ddd")); request.setAttribute("listdata",list); //list[0]表示第一个person对象 %> <br> ${listdata[0].username} <% //servlet产生map数据,存在域中,jsp怎么取值呢? Map map=new HashMap(); map.put("aa", new person("aaaaaaa")); map.put("bb", new person("bbbbbbb")); map.put("cc", new person("ccccccc")); request.setAttribute("map", map); // ${map.bb }表示找到了new person("bbbbb")这个对象,在通过name找出 %> <br> ${map.bb.username} <br>${pageContext.request.contextPath} //获得当前web应用名称 //jstl+El迭达集合中的数据 <br><br> <% List list1=new ArrayList(); list1.add(new person("aaa")); list1.add(new person("bbb")); list1.add(new person("ccc")); list1.add(new person("ddd")); request.setAttribute("listdata",list1); //list[0]表示第一个person对象 %> <c:forEach items="${listdata }" var="person"> ${person.name } </c:forEach>