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 閱讀(2785) 評論(0)  編輯  收藏 所屬分類: Seam
          主站蜘蛛池模板: 西藏| 兰西县| 裕民县| 临江市| 环江| 夏河县| 鲁甸县| 两当县| 梨树县| 临泽县| 遵化市| 枣庄市| 富民县| 巴马| 阿克苏市| 延寿县| 磐石市| 缙云县| 扶沟县| 青川县| 漳州市| 沁水县| 平山县| 安顺市| 广宁县| 饶河县| 会泽县| 徐汇区| 盐亭县| 邯郸市| 凯里市| 化德县| 卓尼县| 盘山县| 宝兴县| 莎车县| 溧水县| 嘉善县| 岳西县| 清流县| 庆元县|