[ Pobierz całość w formacie PDF ]

page for which this bundle can be used.
The var attribute is an alias to the Resources file. This alias can be used
by other tags in the page in order to access the localized messages.
4. Add a key attribute to a component tag to access the particular localized
message and add the bundle attribute to refer to the file containing the
localized message. The bundle attribute must exactly match the var
attribute in the fmt:setBundle tag. Here is an example from more.jsp:
key="OptionsPackages" bundle="carDemoBundle" />
114 USING JAVASERVER FACES TECHNOLOGY
For more information on using the JSTL Internationalization functionality,
please refer to the JavaServer Pages Standard Tag Library topic in The Java Web
Services Tutorial.
Localizing Dynamic Data
The cardemo application has some data that is set dynamically in JavaBean
classes. Because of this, the beans must load this localized data themselves; the
data can t be loaded from the page.
One example of dynamically-loaded data includes the data associated with a
UISelectOnecomponent. Another example is the car description that appears on
the more.jsp page. This description corresponds to the car the user chose from
the Storefront.jsp page. Since the chosen car is not known to the application
prior to startup time, the localized description cannot be loaded from the page.
Instead, theCurrentOptionServerbean must load the localized car description.
In the CurrentOptionServer bean, the localized car title and description is
loaded with the setCarId(int) method, which is called when the user selects a
car from Storefront.jsp. Here is a piece of the setCarId(int) method:
public void setCarId(int id) {
try {
ResourceBundle rb;
switch (id) {
case 1:
// load car 1 data
String optionsOne = "cardemo/CarOptions1";
rb = ResourceBundle.getBundle(
optionsOne,
(FacesContext.getCurrentInstance().getLocale()));
setCarImage("/200x168_Jalopy.jpg");
break;
...
this.setCarTitle((String)rb.getObject("CarTitle"));
this.setCarDesc((String)rb.getObject("CarDesc"));
this.setCarBasePrice((String)rb.getObject("CarBasePrice"));
this.setCarCurrentPrice((String)rb.getObject(
"CarCurrentPrice"));
loadOptions();
}
This method loads the localized data for the chosen car from the
ResourceBundle associated with the car by calling
LOCALIZING MESSAGES 115
ResourceBundle.getBundle, passing in the path to the resource file and the
current locale, which is retrieved from the FacesContext. This method then
calls the appropriate setter methods of the CurrentOptionServer, passing the
locale-specific object representing the localized data associated with the given
key.
The localized data for the UISelectOne components is loaded with the
loadOptions method, which is called when the CurrentOptionServer is ini-
tialized and at the end of the setCarId(int) method. Here is a piece of the
loadOptions method:
public void loadOptions() {
ResourceBundle rb =
ResourceBundle.getBundle("cardemo/Resources",
(FacesContext.getCurrentInstance().getLocale()));
brakes = new String[2];
brakes[0] = (String)rb.getObject("Disc");
brakes[1] = (String)rb.getObject("Drum");
...
brakeOption = new ArrayList(brakes.length);
...
for (i = 0; i
brakeOption.add(new SelectItem(brakes[i], brakes[i],
brakes[i]));
}
Just like in setCarId(int), the loadOptions method loads the localized data
from the ResourceBundle. As shown in the code snippet, the localized data for
the brakes component is loaded into an array. This array is used to create a
Collection of SelectItem instances.
Localizing Messages
The JavaServer Faces API provides a set of classes for associating a set of local-
ized messages with a component. The Message class corresponds to a single
message. A set of Message instances compose a MessageResources, which is
analogous to a ResourceBundle. A MessageResourceFactory creates and
returns MessageResources instances.
MessageResources instances will most commonly comprise a list of validation
error messages. Performing Validation (page 81) includes an example of regis-
tering and using a MessageResources for validation error messages.
116 USING JAVASERVER FACES TECHNOLOGY
To make a MessageResources bundle available to an application, you need to
register the MessageResources instance with the application. This is explained
in Register the Error Messages (page 87).
After registering the MessageResources, you can access the messages from
your application (as explained in Implement the Validator Interface, page 85) by:
1. Calling the getMessageResources(String) method, passing in the
MessageResources identifier
2. Calling getMessage on the MessageResources instance, passing in the
FacesContext, the message identifier, and the substitution parameters.
The substitution parameters are usually used to embed the Validator
properties values in the message. For example, the custom validator
described in Implement the Validator Interface (page 85) will substitute
the format pattern for the {0} in this error message:
Input must match one of the following patterns {0}
Creating Custom UI
Components
If you ve read through the first two chapters of this tutorial, you ve noticed that
JavaServer Faces technology offers a rich set of standard, reusable UI compo-
nents that enable you to quickly and easily construct UIs for Web applications.
But often you need a component with some additional functionality or a com-
pletely new component, like a client-side image map. Although JavaServer
Faces technology doesn t furnish these components in its implementation, its
component architecture allows you to extend the standard components to
enhance their functionality or create your own unique components.
In addition to extending the functionality of standard components, you might
also want to change their appearance on the page or render them to a different
client. Enabled by the flexible JavaServer Faces architecture, you can separate
the definition of the component behavior from its rendering by delegating the
rendering to a separate renderer. This way, you can define the behavior of a cus-
tom component once, but create multiple renderers, each of which defines a dif-
ferent way to render the component.
In addition to providing a means to easily create custom components and render-
ers, the JavaServer Faces design also makes it easy to reference them from the
page through JSP custom tag library technology.
This chapter uses an image map custom component to explain all you need to
know to create simple custom components, custom renderers, and associated
custom tags, and to take care of all the other details associated with using the
components and renderers in an application.
117
118 CREATING CUSTOM UI COMPONENTS
Determining if You Need a Custom
Component or Renderer
The JavaServer Faces implementation already supports a rich set of components
and associated renderers, which are enough for most simple applications. This
section will help you decide if you need a custom component or custom renderer
or if you can use a standard component and renderer.
When to Use a Custom Component
A component class defines the state and behavior of a UI component. This
behavior includes: converting the value of a component to the appropriate
markup, queuing events on components, performing validation, and other func-
tionality.
Situations in which you need to create a custom component include:
" If you need to add new behavior to a standard component, such as gener- [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • lastella.htw.pl
  •