EL表达式输出bean对象

EL表达式输出bean对象

Person.java

package Bean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Person {
    private String name;
    private String[] phone;
    private List<String> cites;
    private Map<String,Object> map;

    public Person() {
    }

    public Person(String name, String[] phone, List<String> cites, Map<String, Object> map) {
        this.name = name;
        this.phone = phone;
        this.cites = cites;
        this.map = map;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getPhone() {
        return phone;
    }

    public void setPhone(String[] phone) {
        this.phone = phone;
    }

    public List<String> getCites() {
        return cites;
    }

    public void setCites(List<String> cites) {
        this.cites = cites;
    }

    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 + '\'' +
                ", phone=" + Arrays.toString(phone) +
                ", cites=" + cites +
                ", map=" + map +
                '}';
    }
}

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="Bean.Person" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    Person p = new Person();
    p.setName("张三");
    p.setPhone(new String[]{"10086","10010","12123"});
    List<String> list = new ArrayList<>();
    list.add("北京");
    list.add("南京");
    list.add("天津");
    list.add("深圳");
    p.setCites(list);
    Map<String,Object> map = new HashMap<>();
    map.put("key1","value1");
    map.put("key2","value2");
    map.put("key3","value3");
    p.setMap(map);
    request.setAttribute("p",p);
%>
${p.name}<br/>
<c:forEach items="${p.phone}" var="item">
 <c:out value="${item}"/>
</c:forEach><br/>

<c:forEach items="${p.cites}" var="item">
 <c:out value="${item}"/>
</c:forEach><br/>
<c:forEach items="${p.map}" var="item">
    <c:out value="${item.key}"/>=<c:out value="${item.value}"/>
</c:forEach>
${fn:length(list)}
</body>
</html>

EL表达式输出bean对象_第1张图片

你可能感兴趣的:(#,Javaweb,java,intellij-idea,tomcat)