EL表达式

EL全名为Expression Language,它原本是JSTL 1.0为方便存取数据所自定义的语言。当时EL只能在JSTL 标签中使用,如:<c:out value="${ 3 + 7}">,程序执行结果为10。但是你却不能直接在JSP 网页中使用:<p>Hi ! ${ username }</p>,到了JSP 2.0 之后,EL 已经正式纳入成为标准规范之一。因此,只要是支持Servlet 2.4 / JSP2.0 的Container,就都可以在JSP 网页中直接使用EL了。除了JSP 2.0 建议使用EL之外,JavaServer Faces( JSR-127 ) 也考虑将EL 纳入规范,由此可知,EL 如今已经是一项成熟、标准的技术。

1、EL表达式主要的作用是从某个范围对象中读取数据,并且将读取到的数据输出到浏览器上。其实不使用EL表达式而使用纯Java代码也能完成读取数据和输出数据,只是为了减少JSP中的Java代码量,使Jsp页面更规范,所以才使用EL表达式。

2、EL表达式的基础语法:
  ${表达式}
  表达式不能为空,必须填写,如果空则无法编译通过。
  EL提供.和[]两种运算符来存取数据。当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 []。如:${user.My-Name}应当改为${user["My-Name"] }

3、关于4个范围对象

JSP中的内置对象的名称对应的java完成类名EL表达式的隐含对象(内置对象)
pageContextjavax.servlet.jsp.PageContextpageScope(页面范围)
requestjavax.servlet.http.HttpServletRequestrequestScope(请求范围)
sessionjavax.servlet.http.HttpSessionsessionScope (会话范围)
applicationjavax.servlet.ServletContextapplicationScope(应用范围)


JSP的:pageContext < request < session < application
JSP中的EL表达式的:pageScope < requestScope < sessionScope < applicationScope
4、JSP的九大内置对象

JSP内置对象名称完整java类名
pageContextjavax.servlet.jsp.PageContext
requestjavax.servlet.http.HttpServletRequest
sessionjavax.servlet.http.HttpSession
applicationjavax.servlet.ServletContext
responsejavax.servlet.http.HttpServletResponse
outjavax.servlet.jsp.JspWriter
exceptionjava.lang.Throwable
pagethis
configjavax.servlet.ServletConfig


5、在page指令中有一个属性isELIgnored,该属性表示“是否忽略EL表达式”,该属性的值是true表示忽略EL表达式,false表示不忽略,缺省值是false,所以一般该属性不需要编写。(除非想忽略EL表达式才会编写此属性)。

6、EL表达式统一都有一个特点:没有取到任何数据则输出“空字符串”,统一都对空值进行了预处理。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!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>EL Test</title>
</head>
<body>

<%-- 表达式不能为空,异常:javax.el.ELException: Failed to parse the expression [${ }] --%>
<%-- ${ } --%>

<%-- 使用java代码向request范围中存储数据 --%>
<% request.setAttribute("username","zhangsan"); %>

<%-- 使用java代码从request范围中读取数据,并且输出到浏览器上 --%>
<%=request.getAttribute("username") %><%-- 使用EL表达式从request范围中读取数据,并且输出到浏览器上 --%>
${requestScope.username }
、
<%-- 细粒度控制:以下EL失效 --%>
\${requestScope.username }


<hr/>
<%-- 使用java代码从request范围中读取数据,并且输出到浏览器上,request中没有存储该数据 --%>
<%=request.getAttribute("usercode") %> <%--输出到浏览器上一个"null" --%><%-- 使用EL表达式从request范围中读取数据,并且输出到浏览器上 ,request中没有存储该数据--%>
${requestScope.usercode} <%-- 输出到浏览器上的是一个空字符串"" --%>


<hr/>
<%-- 严格意义上讲:EL表达式和这样的java代码是相同的 --%>
<%=request.getAttribute("usercode") == null ? "" : request.getAttribute("usercode") %>
、
${requestScope.usercode }


<hr/>
<%-- requestScope可以省略 --%>
${username }


<hr/>
<%
    pageContext.setAttribute("data","pageData");
    request.setAttribute("data","requestData");
    session.setAttribute("data","sessionData");
    application.setAttribute("data","applicationData");
%>

${pageScope.data }
${requestScope.data }
${sessionScope.data }
${applicationScope.data } 

<%-- 如果EL内置范围对象省略的话,恰巧存储在范围中的数据的name是相同的,此时EL表达式会从最小范围中获取数据 --%>
<%-- 小范围优先级较高 --%>
${data}


<hr/>
<%-- 向request范围中存储一个自定义的Emp对象 --%>
<%
    Emp e = new Emp();
    e.setEmpno("111");
    e.setEname("SMITH");

    //将员工对象存储到request范围中
    request.setAttribute("empObj",e);
%>

<%-- 使用EL表达式从request范围中读取Emp对象输出到浏览器上 --%>
${empObj}
\${empObj.a} <%-- 报错:没有getA()方法 --%>
${empObj.empno} <%-- 重点:底层实际上调用了javabean对象的getEmpno()方法,所以只要保证有这个方法就行 --%>
${empObj.ename} <%-- 重点:底层实际上调用了javabean对象的getEname()方法 --%>


<hr/>
<%-- EL表达式另一个隐含对象,内置对象:param --%>
<%=request.getParameter("username") %>
、
${param.username}
、
<%=request.getParameter("username")==null ? "" : request.getParameter("username") %>


<hr/>
<%-- EL表达式从数组中取数据 --%>
<%
    String[] strArray = {"111","222","333","444"};
    request.setAttribute("strArr",strArray);
%>

${strArr}
${strArr[0]}
${strArr[1]}
${strArr[2]}
${strArr[3]}

${strArr[4]} <%-- el表达式从数组中取数据的时候,如果下标越界了,不会出现异常 --%><%
    Emp e1 = new Emp();
    e1.setEmpno("222");
    e1.setEname("KING");

    Emp e2 = new Emp();
    e2.setEmpno("333");
    e2.setEname("Ford");

    Emp[] empArray = {e1,e2};

    request.setAttribute("empArr",empArray);
%>

${empArr[0]["empno"]}
${empArr[0].ename }
、
${empArr[1].empno }
${empArr[1].ename }


<hr/>
<%-- EL表达式的另一个隐含对象,内置对象paramValues --%>
<%-- <%=request.getParameterValues("interest") %><%=request.getParameterValues("interest")[0] %><%=request.getParameterValues("interest")[1] %>、 --%>

${paramValues.interest}、
${paramValues.interest[0]}、
${paramValues.interest[1]}


<hr/>
<%-- 使用EL表达式从List集合中读取数据 --%>
<%
    List<Emp> empList = new ArrayList<Emp>();
    empList.add(e1);
    empList.add(e2);

    request.setAttribute("empList",empList);
%>

${empList}<br/>
${empList[0]}<br/>
${empList[0].empno}<br/>
${empList[0].ename}<br/>

${empList[1]}<br/>
${empList[1].empno}<br/>
${empList[1].ename}<br/>


<hr/>
<%-- 使用EL表达式从Map集合中读取数据 --%>
<%
    Map<String,Emp> empMap = new HashMap<String,Emp>();
    empMap.put("a",e1);
    empMap.put("b",e2);

    request.setAttribute("empMap",empMap);
%>

${empMap }<br/>
${empMap.a }<br/>
${empMap.a.empno }<br/>
${empMap.a.ename }<br/>

${empMap.b }<br/>
${empMap.b.empno }<br/>
${empMap.b.ename }<br/>


<hr/>
<%-- EL表达式另一个隐含对象内置对象 initParam --%>
<%=application.getInitParameter("ips") %>
、
${initParam.ips}


<%--
    总结:EL的隐含对象

        以下四个是专门从范围对象中读取数据:
            pageScope
            requestScope
            sessionScope
            applicationScope

        param  (request.getParameter())
        paramValues (request.getParameterValues())

        initParam  (application.getInitParameter())

        pageContext  
--%>


<hr/>
<%-- 分析以下EL表达式的区别 --%>
${abc} <%-- 从某个范围对象中取数据,name是abc --%>
、
${"abc"} <%-- 将普通字符串"abc"打印输出到浏览器上 --%>


<hr/>
<%-- 关于EL表达式的加法运算 --%>
${1 + 1}
${1 + "2"}
${"10" + "20" }

<%-- EL表达式中的 + 只完成求和,不会做字符串连接,以下程序出现java.lang.NumberFormatException--%>
\${"abc" + "1" }


<hr/>
<%-- 关于EL表达式中 == 调用了equals方法 --%>
<%
    String x = new String("Hello");
    String y = new String("Hello");

    request.setAttribute("s1",x);
    request.setAttribute("s2",y);

%>

<%=(x == y) %>
、
${s1 == s2}


<hr/>
<%-- 关于EL表达式中的eq运算符,和 == 相同,这两个可以替换使用 --%>
${s1 eq s2}
<br/>
\${s1 not eq s2} <%-- 没有这种语法 --%>


<hr/>
<%-- 在 EL表达式中使用三元运算 --%>
${param.age > 18 ? "成年人" : "未成年人"}


<hr/>
<%-- 重点   运算符empty,判断是否为空,什么是空:EL认为只要没有就是空 --%>
<%
    List<String> myList = new ArrayList<String>();
    request.setAttribute("myList",myList);
%>
${empty myList}<%-- true --%>
${empty "" }<%-- true --%>

<%
    request.setAttribute("null","zhangsan");
%>
--${null}--<%-- 输出空串 --%>
${empty null } <%-- true (这里的null还是关键字null,表示空)--%>
${empty requestScope["null"] }<%-- false --%>

<%-- not empty --%>
${not empty myList}<%-- false --%>


<hr/>
<%--
    JSP其中有一个内置对象叫做:pageContext
    对应的完整java类名:javax.servlet.jsp.PageContext
    这个内置对象在EL表达式中对应的隐含对象是:pageContext

    使用EL表达式从pageContext范围中取数据的时候请使用EL表达式中的内置对象pageScope
    如果想调用pageContext内置对象的方法,需要使用EL表达式的内置对象pageContext
--%>
<%-- 使用EL表达式获取base路径 --%>

${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/
<br/>
<%=request.getScheme() %>://<%=request.getServerName() %>:<%=request.getServerPort() %><%=request.getContextPath() %>


<%-- 声明一个内部类 --%>
<%!
public static class Emp {

    private String empno;
    private String ename;

    public Emp(){}

    public String getEmpno() {
        return empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEmpno(String empno) {
        this.empno = empno;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    @Override
    public String toString() {
        return "Emp [empno=" + empno + ", ename=" + ename + "]";
    }
}
%>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值