该篇博客主要关于EL表达式,废话不多说=-=进入正题
在JSP页面的任何静态部分均可通过:${expression}来获取到指定表达式的值
- pageContext
Insert title here
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${name }
可以看出没有使用EL内置对象时查找顺序是由小到大,所以最先显示的是pageContext域中的
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }
可以看出,使用applicationScope即可指定application域中的name输出,当然其他域也是类似,下文会说这四大域属性相关的内置对象
EL有11个内置对象,这里主要讲域属性相关的4个和其他4个
EL的11个内置对象,除了pageContext以外,其他10个内置对象的类型都是java.util.Map类型
applicationScope:从application范围域属性空间中查找指定的key
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }
name=${pageScope.name }
name=${sessionScope.name }
name=${requestScope.name }
该pageContext与JSP内置对象pageContext是同一个对象。通过该对象,可以获取到request、response、session、servletContext、servletConfig等对象注意:这些对象在EL里不是内置对象,这些对象只能通过pageContext获取
Regster.java
package linjie.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Regster extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
name=${param.name }
age=${param.age }
客户浏览器访问结果
获取请求中的指定参数的所以值,其底层实际调用request.getParameterValues()
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%-- ${pageContext.request.contextPath }代表web应用的根 --%>
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
name=${param.name }
age=${param.age }
hobby[0]=${paramValues.hobby[0] }
hobby[1]=${paramValues.hobby[1] }
客户浏览器显示结果
获取初始化参数,其底层调用的是ServletContext.getInitParameter()
web.xml
07eltttt
index.jsp
name
林杰
Regster
Regster
linjie.com.Regster
Regster
/regster
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
name=${initParam.name }
客户浏览器显示结果
JavaBean是公共Java类,但是为了编辑工具识别
需要满足至少三个条件
EL可以通过${key.属性}的方式获取到指定值,其底层实际调用的是该对象的相应属性的get方法
Demo.java
package linjie.com;
/*
*Bean
*/
public class Demo {
private String name;
private int age;
public Demo(String name,int age){
this.name=name;
this.age=age;
}
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;
}
@Override
public String toString() {
return super.toString();
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="linjie.com.Demo"
pageEncoding="UTF-8"%>
Insert title here
<%
Demo test=new Demo("linjie",12);
request.setAttribute("ELttt", test);
%>
name=${requestScope.ELttt.name }
age=${requestScope.ELttt.age }
names=${requestScope.ELtttxx.name }
客户浏览器显示结果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
String[] names={"xlj","lucy","king"};
pageContext.setAttribute("names", names);
%>
name[1]=${names[1] }
names[5]=${names[5] }
下面是访问类的数组
Stu.java
package linjie.com;
/*
*Bean
*/
public class Stu {
private String sname;
private String address;
public Stu() {
super();
}
public Stu(String sname, String address) {
super();
this.sname = sname;
this.address = address;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return super.toString();
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="linjie.com.*"
pageEncoding="UTF-8"%>
Insert title here
<%
Stu[] stus=new Stu[3];
stus[0]=new Stu("xlj","A");
stus[1]=new Stu("lucy","B");
stus[2]=new Stu("kingA","C");
pageContext.setAttribute("stus",stus);
%>
stus[1].Sname=${stus[1].sname }
客户浏览器显示结果
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
List names=new ArrayList();
names.add("xlj");
names.add("lucy");
pageContext.setAttribute("names", names);
%>
names[1]=${names[1] }
客户浏览器显示结果
EL可以通过索引访问List,但无法访问Set。因为Set中没有索引概念
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
Map map=new HashMap();
map.put("age", 20);
map.put("name", "xlj");
pageContext.setAttribute("map", map);
%>
name=${map.name }
age=${map.age }
客户浏览器显示结果
用法为${empty 变量},结果为布尔值
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
String name1=null;
String name2="";
List name3=new ArrayList();
pageContext.setAttribute("name1", name1);
pageContext.setAttribute("name2", name2);
pageContext.setAttribute("name3", name3);
%>
empty对于没有定义的变量,运算结果为true:
empty namex=${empty namex }
empty对于null的引用,运算结果为true:
empty name1=${empty name1 }
empty对于为空串的String引用,运算结果为true:
empty name2=${empty name2 }
empty对于没有元素的数组或集合,运算结果为true:
empty name3=${empty name3 }
客户浏览器显示结果
因为EL本身不具有处理字符串能力,所以可以自定义EL函数
- 定义函数(新建MyEL.java类)
- 注册:先找到jsp2-example-taglib.tld,将头部以及注册函数复制到自己创建的.tld文件中(.tld放在WEB-INF下)
- 在index.jsp中使用,使用时需要<%@ taglib uri=”http://tomcat.apache.org/jsp2-example-taglib” prefix=”MyEL” %>
package linjie.com;
//自定义函数
//该类及其函数,需要在扩展名为.tld的xml文件中注册
//tld:tag library definition(标签库定义)
//xml文件是需要约束的,即需要配置文件头部。这个头部约束可以从一下文件中进行复制
//在Tomcat安装目录下:webapps\examples\WEB-INF\jsp2
//文件为:jsp2-example-taglib.tld
//这个.tld的xml文件,需要定义在当前web项目的WEB-INF目录下,在此目录下创建以.tld结尾的xml文件
//将jsp2-example-taglib.tld中头部复制到创建的xml文件中
//再将函数注册,还是在jsp2-example-taglib.tld底部中复制
public class MyEL {
private static MyEL instance;
public static MyEL getInstance() {
if(instance==null)
{
instance=new MyEL();
}
return instance;
}
//字符串小写变大写
public static String LowerToUpper(String str) {
return str.toUpperCase();
}
}
MyEL.tld
A tag library exercising SimpleTag handlers.
1.0
MyEL
http://tomcat.apache.org/jsp2-example-taglib
MyLowerToUpper
linjie.com.MyEL
java.lang.String LowerToUpper( java.lang.String )
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tomcat.apache.org/jsp2-example-taglib" prefix="MyEL" %>
Insert title here
${MyEL:MyLowerToUpper("sasas") }
<%
String name="xlj";
pageContext.setAttribute("name", name);
%>
${MyEL:MyLowerToUpper(name) }
客户浏览器显示结果