EL表达式
1.EL是JSP内置的表达式语言!
*jsp2.0开始,不让再使用Java脚本,而是使用el表达式和动作标签来替代Java脚本!
*EL替代的是<%= ....%>,也就是说,EL只能做输出!
2.EL表达式来读取四大域
* ${xxx},全域查找名为xxx的属性,如果不存在,输出空字符串,而不是null。
* ${pageScope.xxx}、${requestScope.xxx}、${sessionScope.xxx}、${applicationScope.xxx},指定域获取属性!
1 <body> 2 <% 3 pageContext.setAttribute("xxx", "pageContext_XXX"); 4 request.setAttribute("xxx", "request_XXX"); 5 session.setAttribute("xxx", "session_XXX"); 6 application.setAttribute("xxx", "application_XXX"); 7 8 %> 9 10 ${xxx }<!-- 全域查找,从小到大 --><br> 11 ${pageScope.xxx }<!-- 指定域查找 --><br> 12 ${requestScope.xxx }<!-- 指定域查找 --><br> 13 ${sessionScope.xxx }<!-- 指定域查找 --><br> 14 ${applicationScope.xxx }<!-- 指定域查找 --><br> 15 </body>
请求之后的结果:
3.JavaBean导航
Employee.java:--->一个JavaBean
1 package com.xjs.domain; 2 3 public class Employee { 4 5 private String name; 6 private double salary; 7 private Address address; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public double getSalary() { 15 return salary; 16 } 17 public void setSalary(double salary) { 18 this.salary = salary; 19 } 20 public Address getAddress() { 21 return address; 22 } 23 public void setAddress(Address address) { 24 this.address = address; 25 } 26 @Override 27 public String toString() { 28 return "Employee [name=" + name + ", salary=" + salary + ", address=" 29 + address + "]"; 30 } 31 }
Address.java:--->地址的JavaBean
1 package com.xjs.domain; 2 3 public class Address { 4 5 private String city; 6 private String street;//街道 7 public String getCity() { 8 return city; 9 } 10 public void setCity(String city) { 11 this.city = city; 12 } 13 public String getStreet() { 14 return street; 15 } 16 public void setStreet(String street) { 17 this.street = street; 18 } 19 @Override 20 public String toString() { 21 return "Address [city=" + city + ", street=" + street + "]"; 22 } 23 }
/el/a.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="com.xjs.domain.*" %><!-- 导包 --> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP 'a.jsp' starting page</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 26 <body> 27 <% 28 Address address=new Address(); 29 address.setCity("北京"); 30 address.setStreet("西三旗"); 31 32 Employee emp=new Employee(); 33 emp.setName("金泰妍"); 34 emp.setSalary(9999.00); 35 emp.setAddress(address); 36 37 request.setAttribute("emp", emp); 38 %> 39 <h3>使用el获取request域的emp</h3> 40 <!-- request.getAttribute("emp").getAddress().getStreet() --> 41 <!-- JavaBean导航 --> 42 ${requestScope.emp.address.street } 43 </body> 44 </html>
请求该页面结果:
4.EL可以输出的东西都在11个内置对象中!
EL一个11个内置对象,无需创建即可使用。其中10个是Map类型的!pageContext不是Map,它就是pageContext类型,1个顶9个。
*pageScope
*requestScope
*sessionScope
*applicationScope
*param
*paramValues;
*header;
*headerValues;
*initParam;
*cookie;
*pageContext;
*我们已经学习了四个。
*param:对应参数,它是一个Map,其中key属性名,value是参数值,适用于单值的参数。
1 <body> 2 <!-- map.key这是el的语法! 3 map['key']也可以操作map 4 --> 5 ${param.username } 6 </body>
在浏览器中请求:
*paramValues:对应参数,它是一个Map,其中key参数名,value是多个参数值,适用于多值的参数。
1 <body> 2 <!-- map.key这是el的语法! 3 map['key']也可以操作map 4 --> 5 ${param.username }<br> 6 <!-- 数组 --> 7 ${paramValues.hobby[0] }<br> 8 ${paramValues.hobby[1] } 9 </body>
请求:
*header:对应请求头,它是一个Map,其中key表示头名称,value是单个头值,适用于单值请求头。
1 <body> 2 ${header['User-Agent'] } 3 </body>
请求结果:
*headerValues:对应请求头,它是一个Map,其中key表示头名称,value是多个头值,适用于多值请求头。
*initParam:获取<context-param>内的参数!---平时不用它
在web.xml中
1 <context-param> 2 <param-name>xxx</param-name> 3 <param-value>XXX</param-value> 4 </context-param> 5 <context-param> 6 <param-name>yyy</param-name> 7 <param-value>YYY</param-value> 8 </context-param>
${initParam.xxx}
1 <body> 2 ${initParam.xxx } 3 </body>
请求结果:
*cookie:Map<String,Cookie>类型,其中key是cookie的name,value是cookie对象。使用例如:${cookie.username.value}
1 <body> 2 ${cookie.JSESSIONID.value } 3 </body>
请求结果:
*pageContext:它是PageContext类型!${pageContext.request.scheme}--->相当于调用getRequest().getScheme();获取协议。
<body>
<!-- 项目名 -->
${pageContext.request.contextPath }
<hr>
<a href="${pageContext.request.contextPath }/cookie/a.jsp">点击这里</a>
<form method="post" action="${pageContext.request.contextPath }/el/a.jsp">
<input type="submit" value="xxx">
</form>
</body>
请求之后的结果:
在之后的表单或超链接中的项目名可以这样获取!!!
EL函数库(由JSTL提供)
*导入标签库:<%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " %>
自定义函数库
*写一个java类,类中可以定义0~N个方法,但必须是static ,而且有返回值的!