<h1>获取常量</h1>
${"奥运会"}${2008 } ${true}
<h1>获取域中的变量</h1>
<%
String name="huzi";
pageContext.setAttribute("name", name);
%>
${name }
<h1>获取域中的数组</h1>
<%
String [] names= {"小明","小黄","小新"};
pageContext.setAttribute("names", names);
%>
${names[2]}
<h1>获取map中的数据</h1>
<%
Map<String,String> map = new HashMap();
map.put("name", "aobama");
map.put("age", "47");
pageContext.setAttribute("map", map);
%>
${map["name"]} ${map["age"]}
<h1>获取域中集合中数据</h1>
<%
List<String> list = new ArrayList<String>();
list.add("南山南");
list.add("北上北");
list.add("张磊");
session.setAttribute("list", list);
%>
${list[0] }
<h1>获取javabean中的属性</h1>
<%
Person p = new Person(); //使用"alt+/"来导出包; 在jsp页面中shift+ctrl+o不管用的;
p.setName("习大大");
p.setAge(50);
application.setAttribute("p", p);
%>
${p.name }${p.age } <!--当p.时, El会自动去该类中调用对应的get**方法 -->
Tips:
1.EL表达式能获取的属性必须是存储在四大作用域中的;
2.EL表达式只能获取值不能设置数据;
3.能获取数组中的数据,但不能遍历数组;
<h1>获取javabean中的属性</h1>
<%Person p = new Person(); //使用"alt+/"来导出包; 在jsp页面中shift+ctrl+o不管用的;
p.setName("习大大");
p.setAge(50);
application.setAttribute("p", p);
%>
${p.name }${p.age } <!--当p.时, El会自动去该类中调用对应的get**方法 -->
<!-- p.xxx (对象.属性) -->
当p.xxx时,(对象.属性)
El会自动去该类中调用对应的get***方法 ,
xxx并非是属性,只是p对象中get方法的名字而已(自称为:伪属性),即:getXxx()(get后首字母大写)
demo:
public class User {
private String username;
private String realname;
private String gender;
private String pwd;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String realname, String gender, String pwd) {
super();
this.username = username;
this.realname = realname;
this.gender = gender;
this.pwd = pwd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [username=" + username + ", realname=" + realname
+ ", gender=" + gender + ", pwd=" + pwd + "]";
}
//自定义get方法
public String getHello() {
return "Hello World !";
}
}
${user.hello}----------输出为:Hello World !