Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks
JSF的表达式语言(EL)的形式跟jSP的表达式语言的形式类似:#{...}。EL的主要用途是使你可以引用和更新bean的属性,或者执行简单的语句,而不用写完整的Java代码。
l JSF EL基于JSP 2.0中的表达式语言,它的用法几乎跟JSP2.0中的表达式语言一样,但二者仍有一些关键不同:
l JSF使用(#)来标记表达式的开始,而JSP使用($);
JSF表达式是双向的。即它可以引用属性的值也可以更新之
l JSF EL也允许引用对象方法;
l 某些JSP特定的特征无效,比如页面上下文范围(page);
l JSF EL表达式可以通过常规Java代码求解(结果是可以不需要JSP);
l JSF EL不官方支持函数。
JSF EL也支持“.”和“[]”取值。它支持全范围的算术运算、逻辑运算与关系运算:
1.      算术运算有:
加法 (+), 减法 (-), 乘法 (*), 除法 (/ or div) 与余除 (% or mod) 。下面是算术运算的一些例子:
表达式
结果
#{1}
1
#{1 + 2}
3
#{1.2 + 2.3}
3.5
#{1.2E4 + 1.4}
12001.4
#{-4 - 2}
-6
#{21 * 2}
42
#{3 / 4}
0.75
#{3 div 4}
0.75,除法
#{3 / 0}
Infinity
#{10 % 4}
2
#{10 mod 4}
2,也是求模
#{(1 == 2) ? 3 : 4}
4
如同在Java语法一样 ( expression ? result1 : result2)是个三元运算,expressiontrue显示result1false显示result2
2. 逻辑运算:
and(&&)or(!!)not(!)。一些例子为:
表达式
结果
#{true and false}
false
#{true or false}
true
#{not true}
false
3. 关系运算:
小于Less-than(<、lt)、大于Greater-than (>、gt)、小于或等于Less-than-or-equal(<=、le)、大于或等于Greater-than-or-equal(& gt;=、ge)、等于Equal(==、eq)、不等于Not Equal(!=、ne),由英文名称可以得到lt、gt等运算子之缩写词,以下是一些例子:
表达式
结果
#{1 < 2}
true
#{1 lt 2}
true
#{1 > (4 / 2)}
false
#{1 > (4 / 2)}
false
#{4.0 >= 3}
true
#{4.0 ge 3}
true
#{4 <= 3}
false
#{4 le 3}
false
#{100.0 == 100}
true
#{100.0 eq 100}
true
#{(10 * 10) != 100}
false
#{(10 * 10) ne 100}
false
关系运算也可以用来比较字符或字符串,按字典顺序来决定比较结果,例如:
表达式
结果
#{'a' < 'b'}
true
#{'hip' > 'hit'}
false
#{'4' > 3}
true
4. Empty
用来测试空值(null、空字符串、数组、Map或者没有值的Collection),如:
表达式
结果
#{empty ''}
true
#{empty 'abcd'}
false
JSF EL能搜索Java Web应用的三个范围:application、session、request中以匹配特定关键字的对象。因为JSF一定要锁定到JSP,所以它不支持 page范围。JSF EL支持的隐含变量有:cookie、header、headerValues、initParam、param、paramValues、 applicationScope、sessionScope、requestScope、还有facesContext(当前请求的 FacesContext实例)和view(当前视图)。




JavaServer Faces Expression Language

See Also

This topic is for advanced users who want to enter their own value binding expressions rather than letting the IDE create those expressions. It has the following sections:

Introduction
JavaServer Faces EL Expression Syntax
Get Value Semantics
Set Value Semantics
Implicit Objects
Literals
Operators
Reserved Words

Introduction

JavaServer Faces provides an expression language (JSF EL) that is used in web application pages to access the JavaBeans components in the page bean and in other beans associated with the web application, such as the session bean and the application bean. The IDE in most cases takes care of specifying the correct expression for you, for example, when you bind a component's text property to a data provider or to a JavaBean property.

To bind any property of a component, you can add the component to a page and then right-click the component and choose Property Bindings. You can then use the Property Bindings dialog box to select a property of the component and choose which JavaBeans property the component property is to be bound to.

As an example of binding a component to a database table, the following code sample references a Static Text component. Here's how to produce the code sample:

  1. Drag the Static Text component output text icon from the Basic category of the Palette to a page in the Visual Designer.
     
  2. Open the Servers window and drag the Person table from the Travel database and drop it on the component.
     
    The IDE automatically adds a data provider object for that database table to the page and binds the the text property to the PERSON.PERSONID field of the data provider. You see the text of the component change to 123.
  3. Right-click the component and choose Bind to Data.
     
  4. In the Bind to Data dialog box, choose the PERSON.NAME field of the data provider and click OK to change the binding of the text property to the correct field.
     
  5. Click the JSP button above the page to see the resulting source code.
     

The resulting code in the JSP editor looks like this:

  <ui:staticText binding="#{Page1.staticText1}" 

id="staticText1"

style="position: absolute; left: 216px; top: 192px"

text="#{Page1.personDataProvider.value['PERSON.NAME']}"/>
  • The first line of code shows the name of the JavaServer Faces component, staticText. It uses the qualifier ui:, which identifies the XML namespace for the staticText component. The ui: qualifier is defined in the page header as xmlns:ui="http://www.sun.com/web/ui". This namespace points to a custom tag library for rendering UI components in the Basic, Composite, and Layout categories of the Palette.
     
    There are two other qualifiers that you will see in JSP code that are defined on this same line :
    • h: - Defined in the page header as xmlns:h="http://java.sun.com/jsf/html", this namespace points to a JavaServer Faces custom tag library for rendering JavaServer Faces Reference Implementation components that are primarily in the Standard category of the palette.
       
    • f: - Defined in the page header as xmlns:f="http://java.sun.com/jsf/core", this namespace points to a JavaServer Faces custom tag library for representing event handlers, validators, and other actions.
       

    The TLD documentation for these two qualifiers is located at:
    http://java.sun.com/j2ee/javaserverfaces/1.1/docs/tlddocs/index.html.


     
  • The binding attribute connects this component to a specific JavaBeans object staticText1 in the Page1 page bean. The binding attribute and the attributes id, style, and text are all JavaServer Faces tag library attributes. The last three attributes, id, style, and text, are represented in the IDE as properties of the component and can be set in the component's Properties window.
     
  • The binding and text attributes use the JavaServer Faces expression language. You can use the JavaServer Faces expression language to set the value attribute in the component's Properties window.
     

As described in the sections that follow, the JavaServer Faces expression language syntax uses the delimiters #{}. A JavaServer Faces expression can be a value-binding expression (for binding UI components or their values to external data sources) or a method-binding expression (for referencing backing bean methods). It can also accept mixed literals and the evaluation syntax and operators of the 2.0 expression language.

JavaServer Faces EL Expression Syntax

JSF EL can be used to bind JavaBeans to component properties to simplify how the components access data from various sources. JSF EL expressions use the syntax #{expr};

The syntax of a value binding expression is identical to the syntax of an expression language expression defined in the JavaServer Pages Specification (version 2.0), sections 2.3 through 2.9, with the following exceptions:

  • The expression delimiters for a value binding expression are #{ and } instead
    of ${ and }.
     
  • Value binding expressions do not support JSP expression language functions.
     

In addition to the differences in delimiters, the two expression types have the following semantic differences:

  • During rendering, value binding expressions are evaluated by the JavaServer Faces implementation (via calls to the getValue method) rather than by the compiled code for a page.
     
  • Value binding expressions can be evaluated programmatically, even when a page is not present.
     
  • Value binding expression evaluation leverages the facilities of the configured VariableResolver and PropertyResolver objects available through the Application object for the current web application, for which applications can provide plug-in replacement classes that provide additional capabilities.
     
  • If a value binding expression is used for the value property of an EditableValueHolder component (any input field component), the expression is used to modify the referenced value rather than to retrieve it during the Update Model Values phase of the request processing lifecycle.
     

Examples of valid value binding expressions include:

   #{Page1.name}

#{Foo.bar}

#{Foo[bar]}

#{Foo[“bar”]}

#{Foo[3]}

#{Foo[3].bar}

#{Foo.bar[3]}

#{Customer.status == ‘VIP’}

#{(Page1.City.farenheitTemp - 32) * 5 / 9}

Reporting Period: #{Report.fromDate} to #{Report.toDate}

For value binding expressions where the setValue method is going to be called (for example, for text property bindings for input fields during Update Model Values), the syntax of a value binding expression is limited to one of the following forms, where expr-a is a general expression that evaluates to some object, and value-b is an identifier:

   #{expr-a.value-b}

#{expr-a[value-b]]

#{value-b}

Get Value Semantics

When the getValue method of a ValueBinding instance is called (for example, when an expression on a JSP tag attribute is being evaluated during the rendering of the page), and the expression is evaluated, and the result of that evaluation is returned, evaluation takes as follows:

  • The expression language unifies the treatment of the . and [] operators. expr-a.expr-b is equivalent to a["expr-b"]; that is, the expression expr-b is used to construct a literal whose value is the identifier, and then the [] operator is used with that value.
     
  • The left-most identifier in an expression is evaluated by the VariableResolver instance that is acquired from the Application instance for this web application. If the value on the left side of the . or [] operator is a RowSet, the object on the right side is treated as a column name. See the next section for a more complete evaluation description of these operators.
     
  • Each occurrence of the . or [...] operators in an expression is evaluated by the PropertyResolver instance that is acquired from the Application instance for this web application.
     

  •  
    Properties of variables are accessed by using the . operator and can be nested arbitrarily.
     

Set Value Semantics

When the setValue method of a ValueBinding is called (for example, for text property bindings for input fields during Update Model Values), the syntax of the value binding restriction is restricted as described in the previous section. The implementation must perform the following processing to evaluate an expression of the form #{expra.value-b} or #{expr-a[value-b]}:

  • Evaluate expr-a into value-a.
     
  • If value-a is null, throw PropertyNotFoundException.
     
  • If value-b is null, throw PropertyNotFoundException.
     
  • If value-a is a Map, call value-a.put(value-b, new-value).
     
  • If value-a is a List or an array:
    • Coerce value-b to int, throwing ReferenceSyntaxException on an error.
       
    • Attempt to execute value-a.set(value-b, new-value) or Array.set(value-b, new-value) as appropriate.
       
    • If IndexOutOfBoundsException or ArrayIndexOutOfBoundsException is thrown, throw PropertyNotFoundException.
       
    • If a different exception was thrown, throw EvaluationException.
       

     
  • Otherwise (value-a is a JavaBeans object):
    • Coerce value-b to String.
       
    • If value-b is a writeable property of value-a (as per the JavaBeans Specification), call the setter method (passing new-value). Throw ReferenceSyntaxException if an exception is thrown.
       
    • Otherwise, throw PropertyNotFoundException.
       

     

If the entire expression consists of a single identifier, the following rules apply:

  • If the identifier matches the name of one of the implicit objects described below,
    throw ReferenceSyntaxException.
     
  • Otherwise, if the identifier matches the key of an attribute in request scope,
    session scope, or application scope, the corresponding attribute value will be
    replaced by new-value.
     
  • Otherwise, a new request scope attribute will be created, whose key is the
    identifier and whose value is new-value.
     

Implicit Objects

The expression language defines a set of implicit objects:

  • facesContext - The FacesContext instance for the current request.
     
  • param - Maps a request parameter name to a single value.
     
  • paramValues - Maps a request parameter name to an array of values.
     
  • header - Maps a request header name to a single value.
     
  • headerValues - Maps a request header name to an array of values.
     
  • cookie - Maps a cookie name to a single cookie.
     
  • initParam - Maps a context initialization parameter name to a single value.
     

Objects that allow access to various scoped variables:

  • requestScope - Maps request-scoped variable names to their values.
     
  • sessionScope - Maps session-scoped variable names to their values.
     
  • applicationScope - Maps application-scoped variable names to their values.
     

When an expression references one of these objects by name, the appropriate object is returned. An implicit object takes precedence over an attribute that has the same name. For example, #{facesContext} returns the FacesContext object, even if there is an existing facesContext attribute containing some other value.

Literals

The expression language defines the following literals:

  • Boolean: true and false
     
  • Integer: as in Java
     
  • Floating point: as in Java
     
  • String: with single and double quotes; " is escaped as "", 'is escaped as "', and " is escaped as "".
     
  • Null: null
     

Operators

In addition to the . and [] operators discussed above in Get Value Semantics and the section after that one, the expression language provides the following operators:

  • Arithmetic: +, - (binary), *, /, div, %, mod, - (unary)
     
  • Logical: and, &&, or, ||, not, !
     
  • Relational: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le. Comparisons can be made against other values, or against boolean, string, integer, or floating point literals.
     
  • Empty: The empty operator is a prefix operation that can be used to determine whether a value is null or empty.
     
  • Conditional: A ? B : C. Evaluate B or C, depending on the result of the evaluation of A.
     

The precedence of operators highest to lowest, left to right is as follows:

  • [] .
     
  • ()  (changes precedence of operators)
     
  • - (unary) not ! empty
     
  • * / div % mod
     
  • + - (binary)
     
  • < > <= >= lt gt le ge
     
  • == != eq ne
     
  • && and
     
  • || or
     
  • ? :
     

Reserved Words

The following words are reserved for the expression language and must not be used as identifiers:

and false le not
div ge lt null
empty gt mod or
eq instanceof ne true
See Also
About the JSP Editor
Adding Components to a Page
About Binding Components to Data
Binding Component Properties
About Pages

posted on 2009-02-24 16:02 seal 阅读(2776) 评论(0)  编辑  收藏 所属分类: Seam

只有注册用户登录后才能发表评论。


网站导航: