EL入门

1 篇文章 0 订阅

EL

一:什么是EL表达式?

EL表达式:expression   language
使用EL目的:简化jsp中的Java代码开发
注:EL不是一种开发语言,是jsp中获取数据的一种规范。

二:EL的简单应用

1:获取数据

首先创建一个类

class Person{
	private int age;
	private String name;
	public Person(int age,String name){
		this.age = age;
		this.name = name;
	}
	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;
	}
}

在jsp文件当中:

<%
	Person p = new Person();
	request.setAttribute("person",p);
%>
//以下两种获取方式的结果相同
//第一种获取方式:
${person}
${person.name}
//第二种获取方式:
${person}
${person["name"]}

注:若对象中的某个成员变量是一个对象或者数组,获取方式不变,如下例:

${person.成员变量对象.成员变量属性}
${person.成员变量数组[下标]}
2:运算符
${1 + 1}
${2 - 1}
${2 * 3}
${3 / 2}
${5 % 3}

//运算结果为:
2
1
6
1
2
3:操作数组
${数组名[下标]}
4:操作对象

1)与 1.获取对象同
2)操作jsp内置对象

EL对象/作用域对应关系
pageContext当前的pageContext对象
pageScope对应page作用域
requestScope对应request作用域
sessionScope对应session作用域
applicationScope对应application作用域
param对应request.getParameter()
paraValues对应request.getParameters()
header对应request.getHeader ()
headerValues对应request.getHeaderValues()
cookie对应request.getCookies()
initParam对应ServletContext.getinitParameter()

例:

<%
	request.setAttribute("info","this is request");
	session.setAttribute("info","this is session");
	application.setAttribute("info","this is application");
	Cookie cookie = new Cookie("name","value");
	cookie.setMaxAge(30*60);
	response.addCookie(cookie);
%>
${info}
<hr>
${requestScope.info}<br>
${sessionScope.info}<br>
${applicationScope.info}<br>
${cookie.name.info}

//运行结果为:
this is request 
this is request
this is session
this is application
value 

注:${info}最先会在request找值,若request中不存在值,则会按session、application的顺序去寻找值

5:操作集合

以前面的Person类创建两个对象p1,p2

<%
	List<Person> list = new ArrayList<Person>();
	list.add(p1);
	list.add(p2);
	Map<String,Person> map = new HashMap<String,Person>();
	map.put("1",p1);
	map.put("2",p2);
	request.setAttribute("list",list);
	request.setAttribute("map",map);
%>
//调用方式为:
${list}
${list[1]}
${list[1].name}
${map}
${map["1"]}
${map["1"].name}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值