发布于 2015-08-17 14:43:42 | 185 次阅读 | 评论: 0 | 来源: 网络整理

JSP表达式语言(EL)使得它可以很容易地访问存储在JavaBeans组件的应用程序的数据。 JSP EL中,可以创建两个表达式(A)算术和(B)逻辑。在JSP EL表达式,可以使用整数,浮点数,字符串,内置的常量true和false布尔值,和null.

简单语法:

通常,当指定一个JSP标记的属性值,只需使用一个字符串。例如:


 
<jsp:setProperty name="box" property="perimeter" value="100"/>

JSP EL中,可以指定任何这些属性值的表达式。对于JSP EL中一个简单的语法如下:


 
${expr}

这里 expr 指定表达式本身。在JSP EL中最常用的运算符 . 和 []。这两个操作符允许访问Java Beans各种属性和内置的JSP对象。

例如上述的语法<jsp:setProperty>标记可写的像的表达式:


 
<jsp:setProperty name="box" property="perimeter" 
                 value="${2*box.width+2*box.height}"/>

当JSP编译器看到的${}形式中的一个属性,它会生成代码来计算表达式并替换表达式的值。

您还可以使用JSP EL表达式模板文本中的标记。例如,<jsp:text>标签仅仅插入JSP主体内的内容。下面<jsp:text>声明插入<h1>Hello JSP!</h1>到JSP输出!


 
<jsp:text>
<h1>Hello JSP!</h1>
</jsp:text>

可以包括和使用的属性相同的${}语法的<jsp:text>标签(或任何其他标记)主体一个JSP EL表达式。例如:


 
<jsp:text>
Box Perimeter is: ${2*box.width + 2*box.height}
</jsp:text>

EL表达式可以使用括号将子表达式。例如, ${(1 + 2) * 3} =9, 但是 ${1 + (2 * 3)} =7.

要禁用EL表达式的计算中,我们指定如下page指令的isELIgnored属性:


<%@ page isELIgnored ="true|false" %>

此属性的有效值为true和false。如果为true,EL表达式,当他们出现在静态文本或标签属性被忽略。如果为false,EL表达式是由容器计算。

EL中的基础运算符:

JSP表达式语言(EL)支持绝大多数由Java支持的算术运算和逻辑运算符。下面是最常用的运算符的列表:

Operator 描述
. Access a bean property or Map entry
[] Access an array or List element
( ) Group a subexpression to change the evaluation order
+ Addition
- Subtraction or negation of a value
* Multiplication
/ or div Division
% or mod Modulo (remainder)
== or eq Test for equality
!= or ne Test for inequality
< or lt Test for less than
> or gt Test for greater than
<= or le Test for less than or equal
>= or gt Test for greater than or equal
&& or and Test for logical AND
|| or or Test for logical OR
! or not Unary Boolean complement
empty Test for empty variable values

JSP EL中的函数:

JSP EL中,可以使用表达式的函数也是如此。这些函数必须在自定义标签库来定义。函数用法的语法如下:


 
${ns:func(param1, param2, ...)}

其中ns是函数的命名空间,func是函数的名称,param1是所述第一参数值。例如,函数 fn:length,这是JSTL库的一部分可用于如下来获得字符串的长度。


 
${fn:length("Get my length")}

使用任何标签库(标准或自定义)的函数,则必须在服务器上安装该库,必须使用<taglib>指令,因为在JSTL解释包含在JSP库。

JSP EL隐式对象:

JSP表达式语言支持以下隐式对象:

Implicit object 描述
pageScope Scoped variables from page scope
requestScope Scoped variables from request scope
sessionScope Scoped variables from session scope
applicationScope Scoped variables from application scope
param Request parameters as strings
paramValues Request parameters as collections of strings
header HTTP request headers as strings
headerValues HTTP request headers as collections of strings
initParam Context-initialization parameters
cookie Cookie values
pageContext The JSP PageContext object for the current page

可以在表达式中使用这些对象,好像他们是变量。下面是相关概念几个例子:

pageContext对象:

pageContext对象可以访问pageContext的JSP对象。通过pageContext对象,可以访问请求对象。例如,要访问一个请求传入查询字符串,可以使用表达式:


 
${pageContext.request.queryString}

范围的对象:

 pageScope, requestScope, sessionScope, 以及 applicationScope 变量提供对存储在每个作用域级别的变量。

例如,如果需要明确地访问应用程序范围框变量,可以通过applicationScope变量applicationScope.box访问它。

param和paramValues​​对象:

param和paramValues​​对象使您可以通过request.getParameter和request.getParameterValues​​方法的参数值。

例如,要访问一个名为参数order,使用表达式 ${param.order}  或 ${param["order"]}.

以下是该示例访问名为username请求参数:


 
<%@ page import="java.io.*,java.util.*" %>
<%
    String title = "Accessing Request Param";
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>${param["username"]}</p>
</div>
</body>
</html>

param 对象返回一个字符串值,而paramValues​​对象返回的字符串数组。

header 和 headerValues 对象:

header和headerValues​​对象使可以访问,通常可通过request.getHeader和request.getHeaders方法。

例如,要访问一个名为User-Agent头,使用表达式 ${header.user-agent}或${header["user-agent"]}。

下面是示例访问名为用户代理报头参数:


 
<%@ page import="java.io.*,java.util.*" %>
<%
    String title = "User Agent Example";
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>${header["user-agent"]}</p>
</div>
</body>
</html>

这将显示一些如下:

User Agent Example

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; HPNTDF; .NET4.0C; InfoPath.2)

header 对象返回一个字符串值,而headerValues​​对象返回的字符串数组。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务