package bean; public class Person { public String name; public int age; public Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
package bean; public class Address { public String name="aa我是地址啊"; public String getName() { return name; } public void setName(String name) { this.name = name; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="bean.*,java.util.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>el表达式</title> </head> <body> <% request.setAttribute("name", "aaa"); %> <!-- pageContext.findAttribute("name") page request session application--> ${name } <!-- 在jsp页面中,使用el表达式可以获取bean的属性 --> <% Person p = new Person(); p.setAge(12); request.setAttribute("person", p); %> ${person.age } <!-- 在jsp页面中,使用el表达式可以获取bean中的。。。。。。。。。的属性 --> <% Person person = new Person(); Address address = new Address(); person.setAddress(address); request.setAttribute("person", person); %> ${person.address.name } <!-- 在jsp页面中,使用el表达式获取list集合中指定位置的数据 --> <% Person p1 = new Person(); p1.setName("aa111"); Person p2 = new Person(); p2.setName("bb"); List list = new ArrayList(); list.add(p1); list.add(p2); request.setAttribute("list", list); %> ${list[0].name } <!-- 在jsp页面中,使用el表达式获取map集合的数据 --> <% Map map = new HashMap(); map.put("a", "aaaaxxx"); map.put("b", "bbbb"); map.put("c", "cccc"); map.put("1", "aaaa1111"); request.setAttribute("map", map); %> ${map.a } ${map["1"] } <!-- 利用el表达式获取web应用的名称 --> <a href="${pageContext.request.contextPath }/index.jsp">点点</a> <!-- 注意:如果访问bean不存在的属性,会抛 Property 'username' not found on type cn.itcast.Person --> <%-- ${person.username }--%> </body> </html>
时间: 2024-12-30 19:42:12