ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-08-16 17:00:10  阅读:129  来源: 互联网

标签:EL 存储 return name age 获取 birthday public


EL_获取域中存储的值

  1. el表达式只能从域对象中获取值
  2. 语法
    • ${域名称.键名}:从指定域中获取指定键的值
      • 域名称:
        • pageScope -->pageContext
        • requestScope -->request
        • sessionScope -->session
        • applicationScope -->application(ServletContext)
    • 举例:在request域中存储了name=张三
    • 获取:${requestScope.name}
  •  ${键名}:表示依次从最小的域中查找是否有该对于的值 直到找到为之

jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el获取域中的数据</title>
</head>
<body>
    <%
        //在域中存储数据
        request.setAttribute("name","张三");
        session.setAttribute("age","23");

        
    %>
<h3>el获取值</h3>
${requestScope.name}
${sessionScope.age}
${sessionScope.haha}
</body>
</html>

运行结果

 

 获取域中存储对象值

获取对象、List集合、Map集合的值

  1. 对象:${域名称.键名.属性名}
    • 本质上会去调用对象的getter方法

实体类

package com.bai.dao;

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() {
    }


    public String getBirStr(){
        if (birthday !=null){
            //格式化日期对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //返回字符串即可
            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.bai.dao.User" %>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/8/16
  Time: 16:39
  To change this template use File | Settings | File Templates.
--%>
<%@ 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);
    %>
<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,存储,return,name,age,获取,birthday,public
来源: https://www.cnblogs.com/aimz01/p/16592054.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有