JSF表达式语言

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 )是个三元运算, expression true 显示 result1 false 显示 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

 

转自:http://www.blogjava.net/sealyu/archive/2009/02/24/256444.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSF中,EL(表达式语言)是一种用于访问后台JavaBean的属性和方法的语法。以下是一些常用的EL表达式: 1. 访问JavaBean的属性: ``` #{bean.property} ``` 在上面的示例中,#{bean.property}用于访问JavaBean中的property属性。 2. 调用JavaBean中的方法: ``` #{bean.method()} ``` 在上面的示例中,#{bean.method()}用于调用JavaBean中的method方法。 3. 访问JavaBean中的属性,通过另一个属性或方法: ``` #{bean.property1.property2.property3} #{bean.method1().method2().property} ``` 在上面的示例中,#{bean.property1.property2.property3}用于访问JavaBean中的嵌套属性,#{bean.method1().method2().property}用于通过方法链访问JavaBean中的属性。 4. 在EL表达式中使用运算符: ``` #{bean.property1 + bean.property2} #{bean.property1 > bean.property2} #{bean.property1 == bean.property2} ``` 在上面的示例中,EL表达式中使用了加法、比较等运算符,可以对JavaBean中的属性进行计算和比较。 5. 在EL表达式中使用条件语句: ``` #{bean.visible ? 'visible' : 'hidden'} #{bean.property1 gt bean.property2 ? 'larger' : 'smaller'} ``` 在上面的示例中,EL表达式中使用了三元运算符,可以根据条件返回不同的值。 6. 访问JSF组件的属性: ``` #{component.property} ``` 在上面的示例中,#{}表达式用于访问JSF组件中的property属性。 7. 在EL表达式中使用注释: ``` #{'<!-- My comment -->'} ``` 在上面的示例中,#{}表达式用于在页面上添加注释。 以上是一些常用的EL表达式,但并不是全部,EL表达式JSF中非常重要的一个特性,可以实现页面与后台JavaBean的无缝集成,使得开发Web应用程序变得更加简单和高效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值