编写一个 JSP,利用 EL 读取 JavaBean 中的值。

结构

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值