如何利用EL表达式获取list,map,对象等值
Posted newcityboy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何利用EL表达式获取list,map,对象等值相关的知识,希望对你有一定的参考价值。
<%@ page import="com.hopetesting.domain.User" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el表达式获取对象属性</title>
</head>
<body>
<h1>el设置对象是属性</h1>
<%
User user = new User();
user.setName("zmy");
user.setAge(11);
user.setBirthday(new Date());
request.setAttribute("user", 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","light");
map.put("gender","male");
map.put("user",user);
request.setAttribute("map",map);
%>
<h1>el获取对象属性</h1>
${requestScope.user}<br/>
${requestScope.user.name}<br/>
${user.age}<br/>
${user.birStr}
<h1>el获取list值</h1>
${list[0]}<br/>
${list[1]}<br/>
${list[10]}<br/>
${list[2].name}<br/>
<h1>el获取Map值</h1>
${map.sname}<br/>
${map.gender}<br/>
${map.user.birStr}<br/>
</body>
</html>
package com.hopetesting.domain;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author newcityman
* @date 2019/9/6 - 21:00
*/
public class User {
private String name;
private Integer age;
private Date birthday;
public User() {
}
public User(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public Date getBirthday() {
return birthday;
}
public String getBirStr(){
if(birthday!=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(birthday);
}else{
return "";
}
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"name=‘" + name + ‘‘‘ +
", age=" + age +
", birthday=" + birthday +
‘}‘;
}
}
以上是关于如何利用EL表达式获取list,map,对象等值的主要内容,如果未能解决你的问题,请参考以下文章
JSP ELEL表达式 获取list长度/不用循环,EL在List中直接获取第一项的内容/EL获取Map的键,Map的值