EL表达式

1. 语法结构:

${expression}

2.EL存取器

点(.) : 一般用来访问对象的属性java

${user.id}

方括号([""]) : 一般用于检索数组或集合的元素(当属性名中包含特殊字符,如.或?等并不是字母或数字的符号,就必定要使用[])web

${user["my-name"]}

${user.name}

3.做用域

  • page request session application
  • ${username}:全局搜索

查找顺序:express

①EL表达式中,若不包含任何EL隐式对象的标识符,都被自动假定为引用存储在某个做用域中。数组

②EL会依次检查page request session application浏览器

③找到对应值,则当即返回,不在继续查找cookie

④若没有找到对应值,返回nullsession

4.EL的11个隐式对象

pageContext 
    |pageContext取得用户请求或页面的详细信息
    |
    |-- ${pageContext.request.queryString}        取得请求参数
    |-- ${pageContext.request.requestURL}         取得请求的URL,不包括请求参数(http://localhost:8080/Demo/index.jsp)
    |-- ${pageContext.request.requestURI}		  取得URL的后半部分(/Demo/index.jsp)
    |-- ${pageContext.request.contextPath}        取得服务的web application 的名称(/Demo)
    |-- ${pageContext.request.method}             取得HTTP 的方法(GET、POST)
    |-- ${pageContext.request.protocol}           取得使用的协议(HTTP/1.一、HTTP/1.0)
    |-- ${pageContext.request.remoteUser}         取得用户名称
    |-- ${pageContext.request.remoteAddr }        取得用户的IP 地址
    |-- ${pageContext.session.new}                判断session 是否为新的
    |-- ${pageContext.session.id}                 取得session 的ID
    |-- ${pageContext.servletContext.serverInfo}  取得主机端的服务信息(Apache Tomcat/5.0.28)                      
    |
pageScope requestScope sessionScope applicationScope
    |与做用域一一对应的四个隐式对象
    |
    |--例:session.getAtribute("username") == ${sessionScope.username}
param paramValues 
    |与输入有关的两个对象
    |
    |--例1:request.getParameter(String name) == ${param.name}
    |--例2:request.getParameterValues(String name) == ${paramValues.name}
header headerValues
    |header 储存用户浏览器和服务端用来沟通的数据
    |headerValues 同一标头名称拥有不一样的值,此时必须改成使用headerValues 来取得这些值
    |
    |-- ${header["User-Agent"]}    取得浏览器版本
cookie 
		
initParam
    |web.xml中配置:
    |<context-param>
    |	<param-name>testname</param-name>
    |	<param-value>testvalue</param-value>
    |</context-param>
    |
    |${initParam["testname"]}

5.EL运算符

算术运算符有五个: +、-、*或$、/或div、%或modapp

关系运算符有六个: ==或eq、!=或ne、<或lt、>或gt、<=或le、>=或gejsp

逻辑运算符有三个: &&或and、||或or、!或not函数

其它运算符有三个: Empty运算符、三目运算符

|-${empty param.name} 
    |-${A?B:C}

6.EL函数

须要引入

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

例:${fn:toUpperCase("Www.baidu.Com")}

更多须要查看jstl.jar中的fn.tld文件

7.自定义EL函数

1:建立EL表达式函数处理类, 每一个函数对应类中的一个静态

2:建立tld文件,定义表达式函数

3:在web.xml中配置(可省略)

4:在jsp中导入并使用

7.1 建立处理类
//能够选择继承TagSupport
public class ELtag{
	public static String reverse(String str){
		return new StringBuffer(str).reverse.toString();
	}
}
7.2 建立tld文件
<?xml version="1.0" encoding="GBK"?>  
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"       
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"       
    version="2.0"> 
    
    <!-- 定义函数版本 -->  
   <tlib-version>1.0</tlib-version>  
   <!-- 定义标签类名称 -->  
    <short-name>el</short-name>  
     
	 <!-- 定义函数:reverse -->  
    <function>  
        <name>reverse</name>  
        <function-class>com.java.main.util.Eltag</function-class>  
		
        <!-- 定义函数的对应方法 -->  
        <function-signature>  
            java.lang.String reverse(java.lang.String)  
        </function-signature>  
		
    </function> 
</taglib>
7.3 Web.xml中配置
<jsp-config>  
    <taglib>  
        <!-- 配置标签的引用地址 JSP页面中引用时使用-->  
        <taglib-uri>/eltag</taglib-uri>  
        <!-- 配置标签的TLD文件地址 -->  
        <taglib-location>/WEB-INF/Eltag.tld</taglib-location>  
    </taglib>  
</jsp-config>
7.4 JSP中引入及使用
<%@ taglib uri="/eltag" prefix="el" %> 

${el:reverse("ad") }

禁用EL表达式:

<%@ page isELIgnored="true"%> 表示是否禁用EL语言,JSP2.0中默认的启用EL语言.

还能够在web.xml中配置<el-ignored>元素

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <el-ignored>true</el-ignored>
    </jsp-property-group>
</jsp-config>
相关文章
相关标签/搜索