A typical JavaServer Faces application includes one or more backing beans, which are JavaBeans components (see JavaBeans Components) associated with UI components used in a page. A backing bean defines UI component properties, each of which is bound to either a component''''s value or a component instance. A backing bean can also define methods that perform functions associated with a component, including validation, event handling, and navigation processing.
一個典型的JSF應(yīng)用包括一個或多個后臺bean,在頁面中,這是和UI組件有關(guān)聯(lián)關(guān)系的JavaBeans組件。一個后臺bean定義了UI組件的屬性,每一個屬性都綁定到組件的值或者實例。一個后臺bean也可以定義和組件相關(guān),并執(zhí)行一定功能的方法,包括驗證,事件處理,導(dǎo)航處理。
To bind UI component values and instances to backing bean properties or to reference backing bean methods from UI component tags, page authors use the JavaServer Faces expression language (EL) syntax. This 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 JSP 2.0 expression language (see Expression Language).
如果要綁定UI組件的值和實例到后臺bean的屬性或者通過UI組件的標(biāo)記引用后臺bean的方法,那么頁面開發(fā)者需要使用JSF表達式語言語法。這種語法使用“#{}”指示符。一個JSF的表達式可以是值綁定的(綁定UI組件或者他們的值到外部數(shù)據(jù)源)或者方法綁定(引用后臺bean的方法)。它也可以使用JSP2.0中的語法。
To illustrate a value-binding expression and a method-binding expression, let''''s suppose that the userNo
tag of the guessNumber
application referenced a method that performed the validation of user input rather than using the LongRangeValidator
:
為了演示值綁定表達式和方法綁定表達式,讓我們假設(shè)guessNumber應(yīng)用的userNo標(biāo)記引用了一個方法來作驗證,而不是使用LongRangeValidator。
<h:inputText id="userNo" value="#{UserNumberBean.userNumber}" validator="#{UserNumberBean.validate}" />
This tag binds the userNo
component''''s value to the UserNumberBean.userNumber
backing bean property. It also refers to the UserNumberBean.validate
method, which performs validation of the component''''s local value, which is whatever the user enters into the field corresponding to this tag.
這個標(biāo)記將userNo組件的值綁定到UserNumberBean.userNumber后臺bean的屬性,這個方法對組件的本地值進行了驗證。
The property bound to the component''''s value must be of a type supported by the component. For example, the userNumber
property returns an Integer
, which is one of the types that a UIInput
component supports, as shown in Developing the Beans.
屬性綁定到組件的值必須是組件支持的數(shù)據(jù)類型。例如,userNumber屬性返回一個Integer,這是一個UIInput支持的數(shù)據(jù)類型。
In addition to the validator
attribute, tags representing a UIInput
can also use a valueChangeListener
attribute to refer to a method that responds to ValueChangeEvents
, which a UIInput
component can fire.
除了validator屬性之外,UIInput標(biāo)記還可以使用valueChangeListener屬性來引用一個方法,對應(yīng)于ValueChangeEvents。
A tag representing a component that implements ActionSource
can refer to backing bean methods using actionListener
and action
attributes. The actionListener
attribute refers to a method that handles an action event. The action
attribute refers to a method that performs some processing associated with navigation and returns a logical outcome, which the navigation system uses to determine which page to display next.
一個通過標(biāo)記表現(xiàn)的組件,如果實現(xiàn)了ActionSource,則可以使用actionListener和Action屬性來引用后臺bean的方法。action屬性引用一個方法來處理action事件。action屬性所引用的方法作一些處理,進行頁面導(dǎo)航,并返回一個邏輯結(jié)果,導(dǎo)航系統(tǒng)使用這個邏輯結(jié)果來決定哪個頁面需要被顯示。
A tag can also bind a component instance to a backing bean property. It does this by referencing the property from the binding
attribute:
一個標(biāo)記可以綁定組件的實例到后臺bean的屬性??梢酝ㄟ^從binding的屬性中引用一個屬性來實現(xiàn)。
The property referenced from the binding
attribute must accept and return the same component type as the component instance to which it''''s bound. Here is an example property that can be bound to the component represented by the preceding example inputText
tag:
通過binding屬性引用的屬性必須有和組件實例類型相同的數(shù)據(jù)類型。
UIInput userNoComponent = null;...public void setUserNoComponent(UIInput userNoComponent) { this.userNoComponent = userNoComponent;}public UIInput getUserNoComponent() { return userNoComponent;}
When a component instance is bound to a backing bean property, the property holds the component''''s local value. Conversely, when a component''''s value is bound to a backing bean property, the property holds its model value, which is updated with the local value during the update model values phase of the life cycle.
當(dāng)一個組件實例被綁定到后臺bean的屬性,這個屬性就保存了組件的本地值,相反的,如果一個組件的值被綁定到后臺bean的屬性,則屬性保存的是模式值。在生命周期中的更新模式值的過程中發(fā)生更新本地值的操作。
Binding a component instance to a bean property has these advantages:
綁定一個組件的實例到bean的屬性有下面的一些優(yōu)勢。
In most situations, you will bind a component''''s value rather than its instance to a bean property. You''''ll need to use a component binding only when you need to change one of the component''''s attributes dynamically. For example, if an application renders a component only under certain conditions, it can set the component''''s rendered
property accordingly by accessing the property to which the component is bound.
在大多數(shù)情況下,應(yīng)該盡量使用綁定組件的值而不是組件的實例到bean的屬性。只有當(dāng)你需要動態(tài)的更改組件的屬性時才需要綁定組件的實例到bean的屬性。例如,假如需要在特定的情況下顯示一個組件,那么只需要在后臺bean中訪問綁定的屬性就可以了。
Backing beans are created and stored with the application using the managed bean creation facility, which is configured in the application configuration resource file, as shown in Adding Managed Bean Declarations. When the application starts up, it processes this file, making the beans available to the application and instantiating them when the component tags reference them.
后臺bean的創(chuàng)建和保存是通過可管理的bean的創(chuàng)建工具來完成的,在應(yīng)用的配置文件中進行配置。當(dāng)一個應(yīng)用啟動時,它就處理這個文件,當(dāng)組件的標(biāo)記引用到他們時,使這些bean可用并實例化他們。
In addition to referencing bean properties using value
and binding
attributes, you can reference bean properties (as well as methods and resource bundles) from a custom component attribute by creating a ValueBinding
instance for it. See Creating the Component Tag Handler and Enabling Value-Binding of Component Properties for more information on enabling your component''''s attributes to support value binding.
除了通過value和binding可以引用一個bean的屬性之外,你還可以引用一個bean的屬性(例如方法和資源文件)通過自定義組件的屬性,創(chuàng)建一個ValueBinding實例來完成。