Backing Bean Management
Another critical function of Web applications is proper management of resources. This includes separating the definition of UI component objects from objects that perform application-specific processing and hold data. It also includes storing and managing these object instances in the proper scope.
web应用中另一个关键的功能就是适当的管理资源。这包括将UI组件对象的定义和处理应用程序指定的功能和保存数据的对象分开。它还包括在适当的范围中保存和管理这些对象实例。
A typical JavaServer Faces application includes one or more backing beans, which are JavaBeans components (see java/j2eetutorial14/doc/JSPIntro8.html#wp70711">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应用包括一个或多个后台bean,在页面中,这是和UI组件有关联关系的JavaBeans组件。一个后台bean定义了UI组件的属性,每一个属性都绑定到组件的值或者实例。一个后台bean也可以定义和组件相关,并执行一定功能的方法,包括验证,事件处理,导航处理。
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 java/j2eetutorial14/doc/JSPIntro7.html#wp71019">Expression Language).
如果要绑定UI组件的值和实例到后台bean的属性或者通过UI组件的标记引用后台bean的方法,那么页面开发者需要使用JSF表达式语言语法。这种语法使用“#{}”指示符。一个JSF的表达式可以是值绑定的(绑定UI组件或者他们的值到外部数据源)或者方法绑定(引用后台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:
为了演示值绑定表达式和方法绑定表达式,让我们假设guessNumber应用的userNo标记引用了一个方法来作验证,而不是使用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.
这个标记将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 java/j2eetutorial14/doc/JSFIntro5.html#wp114980">Developing the Beans.
属性绑定到组件的值必须是组件支持的数据类型。例如,userNumber属性返回一个Integer,这是一个UIInput支持的数据类型。
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标记还可以使用valueChangeListener属性来引用一个方法,对应于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.
一个通过标记表现的组件,如果实现了ActionSource,则可以使用actionListener和Action属性来引用后台bean的方法。action属性引用一个方法来处理action事件。action属性所引用的方法作一些处理,进行页面导航,并返回一个逻辑结果,导航系统使用这个逻辑结果来决定哪个页面需要被显示。
A tag can also bind a component instance to a backing bean property. It does this by referencing the property from the binding attribute:
一个标记可以绑定组件的实例到后台bean的属性。可以通过从binding的属性中引用一个属性来实现。
<inputText binding="#{UserNumberBean.userNoComponent}" />
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属性引用的属性必须有和组件实例类型相同的数据类型。
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.
当一个组件实例被绑定到后台bean的属性,这个属性就保存了组件的本地值,相反的,如果一个组件的值被绑定到后台bean的属性,则属性保存的是模式值。在生命周期中的更新模式值的过程中发生更新本地值的操作。
Binding a component instance to a bean property has these advantages:
绑定一个组件的实例到bean的属性有下面的一些优势。
The backing bean can programmatically modify component attributes.
后台bean可以程序化的处理组件的属性。
The backing bean can instantiate components rather than let the page author do so.
后台bean可以例示组件而不是让页面开发者来作这个工作。
Binding a component's value to a bean property has these advantages: 将组件的值绑定到bean的属性有这样一些优势:
The page author has more control over the component attributes.
页面开发者对组件的属性有更多的控制权。
The backing bean has no dependencies on the JavaServer Faces API (such as the UI component classes), allowing for greater separation of the presentation layer from the model layer.
后台bean不必依靠于JSF的API,可以更好的将表现层和模式层分开。
The JavaServer Faces implementation can perform conversions on the data based on the type of the bean property without the developer needing to apply a converter.
JSF可以作数据类型转换而不必有开发者来作一个转换器。
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.
在大多数情况下,应该尽量使用绑定组件的值而不是组件的实例到bean的属性。只有当你需要动态的更改组件的属性时才需要绑定组件的实例到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 java/j2eetutorial14/doc/JSFIntro5.html#wp114997">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的创建和保存是通过可管理的bean的创建工具来完成的,在应用的配置文件中进行配置。当一个应用启动时,它就处理这个文件,当组件的标记引用到他们时,使这些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 java/j2eetutorial14/doc/JSFCustom5.html#wp114102">Creating the Component Tag Handler and java/j2eetutorial14/doc/JSFCustom7.html#wp120558">Enabling Value-Binding of Component Properties for more information on enabling your component's attributes to support value binding.
除了通过value和binding可以引用一个bean的属性之外,你还可以引用一个bean的属性(例如方法和资源文件)通过自定义组件的属性,创建一个ValueBinding实例来完成。