JEE_EL表达式

 

EL表达式语言(Expression Language):在JSP页面中代替Java代码,简化页面,方便代码维护。

 

1.EL基本语法

${expression}。

EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。 因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。

假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。

${表达式}
以前编写jsp代码时,如果要获取表单中的用户名,一般使用  <%=request.getParameter("name")%> ,这样当然也可以获取到值,但是又把html代码和java代码混到一起,看起来比较乱套。现在使用EL表达式的话就比较简洁了:${param.name} 就可以解决了。

注意:${表达式} 表达式部分不需要加引号,如果写成 ${"param.name"} ,那么页面上显示出来的就不是表单中name的值了,而是 "param.name" 这个字符串。表达式与开始符和终结符之间的空格被忽略不计。如果表达式的值为null,那么页面将显示一个空格而不是null。

 

2.禁用EL

(1)使用斜杠“\”

(2)使用page指令:<%@page isELIgnored="true"%>

(3)在web.xml文件中配置<el-ignored>元素

 

3.EL表达式有自身的保留字,避免使用保留字命名。

 

4.EL运算符

通过EL访问数据:

EL 提供“.“和“[ ]“两种运算符来存取数据。

${param.name} 和 ${param["name"]} 是等价的,

但是当要存取的属性名称中包含有一些特殊字符如 "." 或 "-" 等非字母或数字的符号 或者 在使用动态取值时,就一定要使用 "[]" 运算符。

数组元素的获取,List集合元素的获取:

index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>通过EL输出数组的全部元素</title>
</head>
<body>
<%
String[] arr={"Java Web开发典型模块大全","Java范例完全自学手册","JSP项目开发全程实录"};    //定义一维数组
request.setAttribute("book",arr);        //将数组保存到request对象中
%>
<%
String[] arr1=(String[])request.getAttribute("book");    //获取保存到request范围内的变量
//通过循环和EL输出一维数组的内容
for(int i=0;i<arr1.length;i++){
    request.setAttribute("requestI",i);
%>
    
    ${requestI}:${book[requestI]}<br>    <!-- 输出数组中第i个元素 -->
<%} %>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>13.1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

在EL中进行算术运算:

EL会自动转型: ${param.count + 20}

假若窗体传来count的值为10时,那么上面的结果为30。之前没接触过JSP 的读者可能会认为上面的例子是理所当然的,但是在JSP 1.2 之中不能这样做,原因是从窗体所传来的值,它们的类型一律是String,所以当你接收之后,必须再将它转为其他类型,如:intfloat 等等,然后才能执行一些数学运算,下面是之前的做法:

String str_count = request.getParameter("count");

int count = Integer.parseInt(str_count);

count = count + 20;

 

在EL中判断对象是否为空:

<%request setAttribute("user","");%>

<%request.setAttribute("user1",null);%>

${empty user}  ---true

${not empty user1}  ---false

 

在EL中进行逻辑关系运算:

EL关系操作符:==(或eq)、!=(或ne)、<(或lt)、>(或gt)、<=(或le)、>=(或ge)

逻辑操作符:&&(或and)、||(或or)、!(或not)。

View Code
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>关系运算符和逻辑运算符的应用</title>
</head>
<body>
<%
request.setAttribute("userName","mr");     //定义request范围内的变量userName
request.setAttribute("pwd","mrsoft");    //定义pwd范围内的变量pwd
%>
userName=${userName}<br>    <!-- 输入变量userName -->
pwd=${pwd}<br>    <!-- 输入变量pwd -->
\${userName!="" and (userName=="明日") }:    <!-- 将EL原样输出 -->
${userName!="" and userName=="明日" }<br>    <!-- 输出由关系和逻辑运算符组成的表达式的值 -->
\${userName=="mr" and pwd=="mrsoft" }:        <!-- 将EL原样输出 -->
${userName=="mr" and pwd=="mrsoft" }        <!-- 输出由关系和逻辑运算符组成的表达式的值 -->
</body>
</html>

 

在EL中进行条件运算:

${A?B:C}:A结果为true时返回B的结果,否则返回C的结果。

 

 

EL的隐含对象:

1.页面上下文对象:

pageContext:利用此对象可以访问Request、Response、out、Session,exception、page、servletContext等对象。例如:${pageContext.request()}

2.访问作用域范围的隐含对象:

pageScope、

requestScope、

sessionScope、

applicationScope

3.访问环境信息的隐含对象:

param(获取请求中的参数值,例如:${param.name})、 param Values(用于获取请求中的参数值,等同于request.getParameterValues(String name))、 header(用于获取请求报头的值,等同于调ServletRequest.getHeader(String name))、 headerValues(用于获取请求报头的值,等同于调用ServletRequest.getHeaders(String name))、 cookie(用于获取cookie对象)、 initParam(用于获取Web应用程序初始化参数的值)

 

定义和使用EL的函数:

定义函数步骤:

(1)编写一个Java类,并在该类中编写公用的静态方法static。

(2)编写标签库描述文件,该文件扩展名为.tld,保存到Web应用的WEB-INF文件夹下。

(3)在JSP页面中引用标签库<%@taglib uri=标签位置 prefix=调用标签的前缀>,并调用定义的EL函数。

StringDeal.java
package com.wgh;

public class StringDeal {
    public static String shiftEnter(String str) { // 定义公用的静态方法
        String newStr = str.replaceAll("\r\n", "<br>"); // 替换回车换行符
        newStr = newStr.replaceAll(" ", "&nbsp;");// 替换空格符
        return newStr;
    }
}
stringDeal.tld
<?xml version="1.0" encoding="UTF-8"?>
<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
    web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <tlib-version>1.0</tlib-version>
    <uri>/stringDeal</uri>
    <function>
        <name>shiftEnter</name>
        <function-class>com.wgh.StringDeal</function-class>
        <function-signature>java.lang.String shiftEnter(java.lang.String)
        </function-signature>
    </function>
</taglib>
index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>定义EL函数处理字符串中的回车换行符和空格符</title>
</head>
<body>
<form name="form1" method="post" action="deal.jsp">
  <textarea name="content" cols="30" rows="5"></textarea>
  <br>
  <input type="submit" name="Button" value="提交" >
</form>

</body>
</html>
deal.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib uri="/stringDeal" prefix="wghfn"%>
<%request.setCharacterEncoding("GB18030"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>显示结果</title>
</head>
<body>
内容为:<br>
${wghfn:shiftEnter(param.content)}
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>13.5</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

 

 

posted @ 2012-08-14 18:29  汤姆是一只猫  阅读(1468)  评论(0编辑  收藏  举报