今天学习的内容是:使用EL表达式输出Person类中的普通属性、数组属性,list集合属性和map集合属性 。
先创建Person.java:
package demo01; import java.util.Arrays; import java.util.List; import java.util.Map; public class Person { // 需求————输出Person类中的普通属性、数组属性,list集合属性和map集合属性 private String name; private String[] phones; private List<String> cities; private Map<String,Object> map; public Person(String name, String[] phones, List<String> cities, Map<String, Object> map) { this.name = name; this.phones = phones; this.cities = cities; this.map = map; } public Person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getPhones() { return phones; } public void setPhones(String[] phones) { this.phones = phones; } public List<String> getCities() { return cities; } public void setCities(List<String> cities) { this.cities = cities; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } @Override public String toString() { return "Person{" + "name=" + name + ", phones=" + Arrays.toString(phones) + ", cities=" + cities + ", map=" + map + '}'; } }
再创建demo03.jsp:
<%@ page import="demo01.Person" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %><%-- Created by IntelliJ IDEA. User: pc Date: 2021/8/5 Time: 10:17 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% Person person = new Person(); person.setName("哈哈哈"); person.setPhones(new String[]{"12345678910","01379462850"}); List<String> cities = new ArrayList<String>(); cities.add("北京"); cities.add("天津"); cities.add("上海"); person.setCities(cities); Map<String,Object>map = new HashMap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); person.setMap(map); pageContext.setAttribute("p",person); %> 输出Person:${ p }<br> 输出Person的name属性:${p.name}<br> 输出Person的phones数组值:${p.phones[0]}<br> 输出Person的cities集合的元素值:${p.cities}<br> </body> </html>
运行后的页面效果: