EL表达式的出现其实是用来排挤JSP的。因为JSP的代码既有前端html的各种标签,还带有java的逻辑代码,这样就会导致前后端没有完全分离,代码看起来也蛮不爽的。为了解决这个问题,EL表达式横空出世了。
直接举例子吧:
Student类:
public class Student {
private int sno;
private String sname;
private Address address;
public Student() {
}
public Student(int sno, String sname, Address address) {
super();
this.sno = sno;
this.sname = sname;
this.address = address;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Student的级联属性类:
public class Address {
private String homeAddress;
private String schoolAddress;
public Address() {
}
public Address(String homeAddress, String schoolAddress) {
super();
this.homeAddress = homeAddress;
this.schoolAddress = schoolAddress;
}
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
}
现在先在request中加入Student的一个对象
<%
Student student = new Student();
student.setSno(1);
student.setSname("luzelong");
Address address = new Address();
address.setHomeAddress("qq");
address.setSchoolAddress("aa");
student.setAddress(address);
request.setAttribute("student", student);
%>
获取student的信息(下面列出两种方式)
----------------------点操作符-------------------<br/>
${requestScope.student}<br/>
${requestScope.student.sno}<br/>
${requestScope.student.sname}<br/>
${requestScope.student.address.homeAddress}<br/>
${requestScope.student.address.schoolAddress}<br/>
<!-- 中括号包含的字符可以包含特殊字符 - . 、 -->
----------------------[""]操作符-------------------<br/>
${requestScope.student['address']["schoolAddress"]}<br/>
而类比一下jsp的写法:(是不是感觉少了很多代码,而且看起来更爽)
<%
Student student = (Student) request.getAttribute("student");
out.print(student+"<br/>");
out.print(student.getSno()+"<br/>");
out.print(student.getSname()+"<br/>");
//out.print(student.getAddress()+"<br/>");
out.print(student.getAddress().getHomeAddress()+"<br/>");
out.print(student.getAddress().getSchoolAddress()+"<br/>");
%>
上面打印的结果都是:
org.student.entity.Student@6624f648
1
luzelong
qq
aa
给session域中放一个数组和Map:
String[] POW= new String[] {"AA","BB","CC"};
session.setAttribute("POW", POW);
Map<String,Object> map = new HashMap<>();
map.put("cf","穿越火线");
map.put("cs", "反恐精英");
session.setAttribute("map",map);
EL取出上面的单个值
${sessionScope.POW[0]}<br/>
${sessionScope.POW[1]}<br/>
${sessionScope.POW[2]}<br/>
${sessionScope.map.cf}<br/>
${sessionScope.map["cs"]}<br/>
输出结果为:
AA
BB
CC
穿越火线
反恐精英
EL的简单判断:
${9>1}、${9 gt 2}、${9>2 || 9<2}、${9>2 or 9<2} <br/>
上面的都为true!
Empty运算符:对于不存在的对象以及为null返回为true:(比如下面的表达式中request域中根本就没有DDD,所以返回是true)
${empty requestScope["DDD"]}
获取地址的栏的参数
比如我要访问的是 www.luzelong.com?test=1
地址栏的test值怎么获取???
${test} ${requestScope.test} <%request.getAttribute("test"); %>
以上的方法均不能获取。
只能通过下面的方法获取:
${param.test}
输出结果为1
其实EL还可以获取JSP隐式对象,比如获取服务器Tomcat的端口:
${pageContext.request.serverPort}
看完这篇相信各位对el基本掌握了,希望点个赞关注一下吧_