首页 > 其他分享 >获取域中存储的值和获取域中存储的值对象值

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

时间:2022-08-15 15:38:06浏览次数:44  
标签:存储 name 对象 age 获取 birthday public

获取域中存储的值

1.el表达式只能从域对象中获取值

2.语法:

  1.${域名称.键名}:从指定域中获取指定键的值

    域名称:

      1.pageScope     --> pageContext

      2.requestScope      --> request

      3.sessionScope      --> session

      4.applicationScope   --> application(ServletContext)

    举例:在request域中存储了name = 张三

    获取:${requestScope.name}

  2.${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。

    ${name}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el获取域中的数据</title>
</head>
<body>

    <%
        //在域中存储数据
        session.setAttribute("name","李四");

        request.setAttribute("name","张三");
        session.setAttribute("age","23");


    %>

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

${name}
</body>
</html>

 

获取域中存储的值对象值

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

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

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

User对象

package com.peng.request.demo.domain;

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

public class User {
    private String name;
    private int age;
    private Date birthday;

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

    public User() {
    }

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

    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;
    }


}

js页面:

<%@ page import="com.peng.request.demo.domain.User" %>
<%@ page import="java.util.Date" %>
<%@ 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}

</body>
</html>

 

 

搜索

复制

标签:存储,name,对象,age,获取,birthday,public
From: https://www.cnblogs.com/pengtianyang/p/16588456.html

相关文章