EL表达式和EL函数库,EL内置对象
其他
2018-08-11 05:16:16
阅读次数: 0
EL表达式
- 1. EL是JSP内置的表达式语言!
- * jsp2.0开始,不让再使用java脚本,而是使用el表达式和动态标签来替代java脚本!
- * EL替代的是<%= ... %>,也就是说,EL只能做输出!
-
- 2. EL表达式来读取四大域
- * ${xxx},全域查找名为xxx的属性,如果不存在,输出空字符串,而不是null。
- * ${pageScope.xxx}、${requestScope.xxx}、${sessionScope.xxx}、${applicationScope.xxx},指定域获取属性!
-
- 3. javaBean导航
- <%
- Address address = new Address();
- address.setCity("北京");
- address.setStreet("西三旗");
-
- Employee emp = new Employee();
- emp.setName("李小四");
- emp.setSalary(123456);
- emp.setAddress(address);
-
- request.setAttribute("emp", emp);
- %>
-
- <h3>使用el获取request域的emp</h3>
- ${requestScope.emp.address.street }<!-- request.getAttribute("emp").getAddress().getStreet() --><br/>
-
-
- 4. EL可以输出的东西都在11个内置对象中!11个内置对象,其中10个是Map!pageContext不是map,它就是PageContext类型,1个项9个。
- * 我们已经学习了四个
- * param:对应参数,它是一个Map,其中key参数名,value是参数值,适用于单值的参数。
- * paramValues:对应参数,它是一个Map,其中key参数名,value是多个参数值,适用于多值的参数。
- * header:对应请求头,它是一个Map,其中key表示头名称,value是单个头值,适用于单值请求头
- * headerValues:对应请求头,它是一个Map,其中key表示头名称,value是多个头值,适用于多值请求头
- * initParam:获取<context-param>内的参数!
- <context-param>
- <param-name>xxx</param-name>
- <param-value>XXX</param-value>
- </context-param>
- <context-param>
- <param-name>yyy</param-name>
- <param-value>YYY</param-value>
- </context-param>
-
- ${initParam.xxx}
-
- * cookie:Map<String,Cookie>类型,其中key是cookie的name,value是cookie对象。 ${cookie.username.value}
- * pageContext:它是PageContext类型!${pageContext.request.contextPath}
2.EL函数库
- * 导入标签库:<%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
- String toUpperCase(String input):把参数转换成大写
- String toLowerCase(String input):把参数转换成小写
- int indexOf(String input, String substring):从大串,输出小串的位置!
- boolean contains(String input, String substring):查看大串中是否包含小串
- boolean containsIgnoreCase(String input, String substring):忽略大小写的,是否包含
- boolean startsWith(String input, String substring):是否以小串为前缀
- boolean endsWith(String input, String substring):是否以小串为后缀
- String substring(String input, int beginIndex, int endIndex):截取子串
- String substringAfter(String input, String substring):获取大串中,小串所在位置后面的字符串
- substringBefore(String input, String substring):获取大串中,小串所在位置前面的字符串
- String escapeXml(String input):把input中“<”、">"、"&"、"'"、""",进行转义
- String trim(String input):去除前后空格
- String replace(String input, String substringBefore, String substringAfter):替换
- String[] split(String input, String delimiters):分割字符串,得到字符串数组
- int length(Object obj):可以获取字符串、数组、各种集合的长度!
- String join(String array[], String separator):联合字符串数组!
3.EL内置对象
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<!-- 四大作用域对象(jsp pageContext request session appliation 两个方法setAtrribute 和getAttribute)
pageContext.setArrtibute("my",new MyCard())
-->
<jsp:useBean id="user" class="com.et.User" scope="page"></jsp:useBean>
<jsp:setProperty property="id" name="user" value="1"/>
<jsp:setProperty property="caridNo" name="user" value="123"/>
<!-- el表达是也拥有内置对象pageScope requestScope sessionScope applicationScope -->
${pageScope.user.id }
<jsp:useBean id="user1" class="com.et.User" scope="page"></jsp:useBean>
<jsp:setProperty property="id" name="user1" value="11"/>
<jsp:setProperty property="caridNo" name="user1" value="123"/>
${pageScope.user1.id }
<br/>
<!-- 如果不指定作用域对象 会从作用域最低的对象搜索 pageContext<request<session<applicition
不指定作用域会影响效率
-->
<!-- jsp内置对象pageContext request session application
userbean的sope page request session application
el表达是内置对象pageScope requestScope sessionScope applicationScope
el表达是内置对象作用域对象实际上就是个map 只是填充了通过setAtrribute()方法写入的键值对
-->
<%
pageContext.setAttribute("id", "1");
request.setAttribute("id", "2");
session.setAttribute("id", "3");
application.setAttribute("id", "4");
%>
${sessionScope }
${pageContext.request.contextPath }
${pageContext.request.session.id }
<!-- 使用param内置对象获取url上的参数
param == request.getParmeter("id")
paramVlues== request.getParmeter("id")
-->
${param.id }
${paramValues.id }
${header.Connection }
${header['Accept-Language'] }
${cookie.JSESSIONID.value }
</body>
</html>
转载自blog.csdn.net/XCL18215166914/article/details/79006452