首页 > 其他分享 >EL_获取域中存储的值-对象值、EL_获取域中存储的值-List集合&Map集合值

EL_获取域中存储的值-对象值、EL_获取域中存储的值-List集合&Map集合值

时间:2023-02-11 13:33:59浏览次数:47  
标签:EL 存储 name map age list birthday 集合 public

EL_获取域中存储的值-对象值

  1.对象:${域名称.键名.属性名}

    本质上会去调用对象的getter方法

User类

package com.example.el;

import java.text.SimpleDateFormat;
import java.util.Date;

public class User {

    private String name;
    private int age;
    private Date birthday;

    public User(String name, int age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public User() {
    }

    /**
     * 逻辑视图
     * @return
     */
    public String geiBirStr() {

        if (birthday != null) {
            // 1.格式化日期对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 2.返回字符串即可
            return sdf.format(birthday);
        } else {
            return "";
        }

    }

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

JSP

<%@ page import="com.example.el.User" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el获取数据</title>
</head>
<body>

    <%
        User user = new User();
        user.setName("张三");
        user.setAge(23);
        user.setBirthday(new Date());

        request.setAttribute("u",user);

        List list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add(user);

        request.setAttribute("list",list);

        Map map = new HashMap<>();
        map.put("sname","李四");
        map.put("gender","男");
        map.put("user",user);

        request.setAttribute("map",map);
    %>

    <h3>el获取对象中的值</h3>
    ${requestScope.u}<br>

    <%--
        通过的是对象的属性来获取
            setter或getter方法,去掉set或get,再将剩余部分,首字母变为小写。
            setName --> Name --> name
    --%>

    ${requestScope.u.name}<br>
    ${u.age}<br>
    ${u.birthday}<br>
    ${u.birthday.month}<br>

    ${u.birStr}<br>

</body>
</html>

EL_获取域中存储的值-List集合&Map集合值

List集合、Map集合的值

  List集合:

    ${域名称.键名[索引]}

  Map集合:

    ${域名称.键名.key名称}

    ${域名称.键名["key名称"]}

<%@ page import="com.example.el.User" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el获取数据</title>
</head>
<body>

    <%
        User user = new User();
        user.setName("张三");
        user.setAge(23);
        user.setBirthday(new Date());

        request.setAttribute("u",user);

        List list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add(user);

        request.setAttribute("list",list);

        Map map = new HashMap<>();
        map.put("sname","李四");
        map.put("gender","男");
        map.put("user",user);

        request.setAttribute("map",map);
    %>

    <h3>el获取对象中的值</h3>
    ${requestScope.u}<br>

    <%--
        通过的是对象的属性来获取
            setter或getter方法,去掉set或get,再将剩余部分,首字母变为小写。
            setName --> Name --> name
    --%>

    ${requestScope.u.name}<br>
    ${u.age}<br>
    ${u.birthday}<br>
    ${u.birthday.month}<br>

    ${u.birStr}<br>

    <h3>el获取List值</h3>
    ${list}<br>
    ${list[0]}<br>
    ${list[1]}<br>
    ${list[10]}<br>

    ${list[2].name}

    <h3>el获取Map值</h3>
    ${map.gender}<br>
    ${map["gender"]}<br>
    ${map.user.name}

</body>
</html>

标签:EL,存储,name,map,age,list,birthday,集合,public
From: https://www.cnblogs.com/wsfj/p/17111294.html

相关文章