结构
Adress.java
package com.beans;
public class Address {
private String city;
public String getCity(){
return city;
}
public void setCity(String city){
this.city = city;
}
}
Profile.java
package com.beans;
import java.util.Date;
import java.util.Map;
public class Profile {
private String email;
private Date birthday;
private Address[] address;
private Map<String, String> phone;
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday){
this.birthday = birthday;
}
public Address[] getAddress() {
return address;
}
public void setAddress(Address[] address){
this.address = address;
}
public Map<String, String> getPhone() {
return phone;
}
public void setPhone(Map<String, String> phone){
this.phone = phone;
}
}
User.java
package com.beans;
public class User {
private Long userID;
private String userName;
private String password;
private Profile profile;
public Long getUserID(){
return userID;
}
public void setUserID(Long userID){
this.userID = userID;
}
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;
}
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
index.jsp
<%@ page import="com.beans.Profile" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.beans.Address" %><%--
<%@ page import="com.beans.User" %><%--
Created by IntelliJ IDEA.
User: 32358
Date: 2023/5/26
Time: 9:26
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>
<!-- 使用User Bean,设置属性值username -->
<jsp:useBean id="user" class="com.beans.User"></jsp:useBean>
<jsp:setProperty name="user" property="userName" value="meixin"/>
<%
//建立Profile对象,设置邮件地址
Profile p = new Profile();
p.setEmail("wnight88@sina.com");
//将不同的电话存入Map中,并设置在p对象的属性中
Map<String,String> phone = new HashMap<String,String>();
phone.put("office","8383838");
p.setPhone(phone);
//建立地址对象,设置城市名
Address address = new Address();
address.setCity("北京");
Address[] addresses = {address};
p.setAddress(addresses);
user.setProfile(p);
%>
<!-- 用JSP EL的级连方式输入值 -->
<!-- 输出user对象中的username属性值,三种写法等价 -->
${user.userName }<br>
${user["userName"] }<br>
${user['userName'] }<br>
<!-- 输出user对象中profile属性对象中的phone属性Map中键值为office的值 -->
${user.profile.phone.office }<br>
${user['profile']['phone']['office'] }<br>
<!-- 输出user对象中profile属性对象中address数据属性中第0个元素对象中的city的属性
值 -->
${user.profile.address[0].city }<br>
</body>
</html>