1.EL 表达式概述
EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写。
2.EL从域中取出数据(EL最重要的作用)
jsp脚本:
<%=request.getAttribute(name)%>
EL表达式替代上面的脚本:${requestScope.name}
EL最主要的作用是获得四大域中的数据,格式${EL表达式}
EL获得pageContext域中的值:${pageScope.key};
pageContext域是pageScope 而不是pageContextScope
EL获得request域中的值:${requestScope.key};
EL获得session域中的值:${sessionScope.key};
EL获得application域中的值:${applicationScope.key};
**
EL从四个域中获得某个值${key};
—同样是依次从pageContext域,request域,session域,application域中获取属性,在某个域中获取后将不在向后寻找1)获得普通字符串
<%
request.setAttribute("name", "张三");
%>
${name }
2)获得User对象的值
3)获得List的值
下标从0开始
3.EL的内置对象(11个)
pageScope/requestScope/sessionScope/applicationScope
---- 获取JSP中域中的数据
param,paramValues
- 接收参数. 相当于request.getParameter() 和 request.getParameterValues()
header,headerValues
- 获取请求头信息相当于request.getHeader(name)
initParam
- 获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)
cookie -WEB开发中cookie
相当于request.getCookies()---cookie.getName()---cookie.getValue()
pageContext
- WEB开发中的pageContext. pageContext获得其他八大对象
这里可以获得request对象 在EL内置对象没有Request对象,只有RequestScope域对象
${pageContext.request.contextPath}
相当于
<%=pageContext.getRequest().getContextPath%> 这句代码不能实现 获得WEB应用的名称
因为getRequest()是获得的ServletRequest() 而没有获取ContextPath这个方法
EL表达式获取的HTTPServletRequest() 有获取ContextPath这个方法主要用法就是获取Web应用的名称 这样修改Web应用名称也不需要改动
4.EL执行表达式
例如:
${1+1}
结果为 2
${empty user}
看user是否为空 看的是域里面是否为空 这里为false
${user==null?true:false}