EL获取域中存储的值以及获取域中存储的对象值
EL获取域中存储的值
1、el表达式只能从域对象中获取值
2、语法:
1、${域名城.键名}:从指定域中获取指定键的值
域名城:
1、pageScope ——>pageContext
2、requestScope ——>request
3、sessionScope ——>session
4、applicationScope ——>application(ServletContext)
<% //在域中存储数据 request.setAttribute("name","张三"); session.setAttribute("age","20"); %> <h3>el获取值</h3> ${requestScope.name} ${sessionScope.age}
2、${键名}:表示依次从最小的域中查找是否有该键对应的值直到找到为止。
<% //在域中存储数据 request.setAttribute("name","张三"); session.setAttribute("age","20"); %> <h3>el获取值</h3> ${requestScope.name} ${sessionScope.age} ${name}
EL获取域中存储的对象值
语法:${域名城.键名.属性名}
本质上会去调用对象的getter方法
User实体类:
public class User { private String name; private int age; private Date birthday; public String getBitstr() { if(birthday!=null){ //1、格式化日期对象 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2、返回字符串 return df.format(birthday); }else { return ""; } } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", birthday=" + birthday + '}'; } 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; } }
jsp页面:
<% User user = new User(); user.setName("张三"); user.setAge(30); user.setBirthday(new Date()); request.setAttribute("u",user); %> <h3>获取对象中的值</h3> ${requestScope.u} <br> <%-- 通过的是对象的属性来获取 setter或getter方法,去掉set或get,再将剩余部分首字母变为小写 setName ——> Name ——> name --%> ${requestScope.u.name}<br> ${u.age}<br> ${u.bitstr}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示