访问Map对象的元素值 ${map.key}
访问Collection对象的元素值${collection[i]}
访问数组的元素值${array[i]}
【index.jsp】
<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
HashMap<String,String> map1 = new HashMap<String,String>();
map1.put("tom","maomao");
map1.put("jerry","shushu");
map1.put("xiaoming","pangzi");
map1.put("a b","aaaaa");
map1.put("{}","123");
//pageContext.setAttribute("m1",map1);
request.setAttribute("m1",map1);
LinkedList<String> list = new LinkedList();
list.add("abc");
list.add("hello");
pageContext.setAttribute("list1",list);
%>
${m1['tom']}<br/>
${m1["jerry"]}<br/>
${m1.xiaoming }<br/>
${m1.tom }<br/> //建议这样写
${m1["a b"]}<br/> //特殊情况只能这样写
${m1["{}"]}<br/>
${list1[1] }<br/>
</body>
</html>
访问JavaBean的属性值${bean.attr}。注意javabean的属性名字是根据get、set的名字第一个字母小写得到的,和private变量的名字无关
【Dog.java】
package com.rupeng.web3;
public class Dog
{
private int hahaId;
private String name;
private Person master;
private String birthDay;
public String getBirthDay()
{
return birthDay;
}
public void setBirthDay(String birthDay)
{
this.birthDay = birthDay;
}
public int getId()
{
return hahaId;
}
public void setId(int id)
{
this.hahaId = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Person getMaster()
{
return master;
}
public void setMaster(Person master)
{
this.master = master;
}
}
【Person.java】
package com.rupeng.web3;
public class Person
{
private int id;
private String name;
private int age;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
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;
}
}
【注意】JavaBean的属性是getId 中的id 并不是private字段
el访问N个对象(*)
el内置了N个和jsp有关的map对象
pageScope -- page域对象中保存属性的map, pageContext.getAttribute ("key")
requestScope -- request域对象中保存属性的map,request..getAttribute ("key")
sessionScope -- session域对象中保存属性的map
applicationScope -- ServletContext域对象中保存属性的map
param -- request的所有请求参数的map ,request.getParameter("key")
paramValues -- request的所有请求参数值的map ,request.getParameterValues("key")
header -- request的所有请求头的map
headerValues-- request的所有请求头的map
cookie -- request的所有请求cookie的map
initParam -- 在web.xml中配置的初始化参数,servletContext.getInitparameter(Stringname)
例子:${requestScope.map1.rupeng}
对于pageScope , requestScope,sessionScope,applicationScope 这四个map对象 , 有一个统一简单的访问方式${key},el会依次访问这四个map对象直到找到key对应的value
【el1.jsp】
<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.rupeng.web3.Person"%>
<%@page import="com.rupeng.web3.Dog"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Person p1 = new Person();
p1.setId(3);
p1.setName("tom");
p1.setAge(3);
Dog d1 = new Dog();
d1.setId(1);
d1.setName("wangcai");
d1.setMaster(p1);
d1.setBirthDay("2016-10-10");
pageContext.setAttribute("wangcai",d1);
pageContext.setAttribute("test","hell1");
request.setAttribute("test","hello2");
session.setAttribute("test","hello3");
application.setAttribute("test","hello4");
%>
name:${param.name}<br/>
UserAgent:${header["User-Agent"]}<br/>
${test}<br/>
${sessionScope.test}<br/>
${applicationScope.test}<br/>
${requestScope.test}<br/>
${wangcai.name}<br/>
${wangcai.id}<br/>
${wangcai.birthDay }<br/>
${wangcai.master.name}<br/>
</body>
</html>
从上面的格式可以看出 , el有两种访问数据的方式 . 和 [ ]
. 支持的 [ ] 都支持 , ${xx.xxx } 等价于 ${xx["xxx"]}或${ xx['xxx']}
[ ]支持动态访问map["aa"],map[aa]
[ ]还支持含有特殊字符的属性map["aa bb"],必须用[""]了