Table of Contents Berner Fachhochschule, Technik und Informatik Life-cycle � Basic JSF Life-cycle Conversion and validation Advanced Web Technologies Invoke Application 6) JSF Lifecycle, Event handling, Actors in the JSF process data binding, i18n Internationalization - I18n � Motivations I18n in Java (very short presentation) Dr. E. Benoist Change Language I18n in JSF Fall Semester 2010/2011 Conclusion � Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n 1 2 Basic JSF Lifecycle Life-cycle ◮ Create the component tree ◮ Map values to the component nodes (corresponding to request parameters) • Request parameters are stored in the component themselves ◮ Process validations Validate each of the obtained values ◮ Update Model Values Value is copied inside the corresponding backing beans. ◮ Invoke Application : Event handling and actions are executed. ◮ Render Response • A new component tree is created (if needed by navigation) • Backing beans values are transferred back to Components • Components are rendered Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Life-cycle: Basic JSF Life-cycle Life-cycle: Basic JSF Life-cycle 3 4
Apply Request Value Apply Request Value (Cont.) Use Converters ◮ Can be the default JSF converters < ! −− UserRegistration.jsp −− > < h:inputText id=”age” value=”# { UserRegistration.user. ց ◮ Submitted form values are stored inside the component → age } ” > ◮ Value is converted < f:converter id=”javax.faces.Short”/ > < /h:inputText > • The component contains one type of input • Input is always sent as a “String” ◮ Or a Date default converter • JSF must convert the value into the right type < ! −− UserRegistration.jsp −− > • Conversion may be implicit or explicit < h:inputText id=”birthDate” value=”# { UserRegistration.user.birthDate } ” ց → > < f:convertDateTime pattern=”MM/yyyy”/ > < /h:inputText > Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Life-cycle: Conversion and validation Life-cycle: Conversion and validation 5 6 Define your own converter Validation ◮ Once the value is inside each component, it is validated ◮ Create a Converter class < h:inputText id=”age” value=”# { UserRegistration.user. ց import javax.faces.convert.Converter; public class PhoneConverter implements Converter { → age } ” > < f:validateLongRange maximum=”150” public Object getAsObject(FacesContext context, UIComponent component, String value) { ց minimum=”0”/ > < /h:inputText > → ... } public String getAsString(FacesContext context, ◮ If conversion or validation fails: UIComponent component, Object value) ց • A message is generated (printed in the message tag) → { ... } • The life-cycle goes directly to the rendering step } • The page is rendered directly (other steps are not executed) • Values are never transferred to the backing beans in this case. ◮ Declare its use in the faces-config.xml file ◮ If conversion and validation succeed: ◮ Use the declared name in the JSP File • Value is transferred to the corresponding Backing bean. < h:inputText id=”phone” • Backing beans “getter” methods are executed using values value=”# { UserRegistration.user.phone } ” > stored in the components < f:converter converterId=”arcmind.PhoneConverter” / > Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n < /h:inputText > Life-cycle: Conversion and validation Life-cycle: Conversion and validation 7 8
Invoke Application: Event handling Rendering and actions ◮ The values in the beans are stored back in the tree • The component accesses the getter method of the bean ◮ After the value is transferred to ALL backing beans ◮ Each component know how it should be rendered ◮ Events are fired by the component ◮ Some render themselves directly • Value Changed Events : for input fields • Using the encoding functions: encodeBegin(...) , • Action Event : for command components encodeChildren(...) and encodeEnd(...) ◮ EL expressions corresponding to actions are computed ◮ Some use an external renderer (or many depending on the application) • They can use all the information stored inside the backing beans • The encoding and decoding functionalities are delegated to a ◮ Action values are used to fire a navigation rule “Renderer” Class. • The View page is computed according to the returned value public class FieldRenderer extends Renderer { Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Life-cycle: Invoke Application Life-cycle: Invoke Application 9 10 Basic JSF Life-cycle Actors in the JSF process (the same picture) ◮ Components : • Compose the component tree (nodes have one parent and can have many children) • Are the center piece of our puzzle ◮ Managed Beans • Contain the values stored by the application • Contain the link with the business layer ◮ Renderer • “decode” the form input • “encode” the component Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Life-cycle: Invoke Application Life-cycle: Actors in the JSF process 11 12
Actors in the JSF process (Cont.) Actors Managed Beans Tag‐ library ◮ Tag-lib • xml definition of the tag and its arguments ◮ Tag class • The class instanciated by the tag lib. Tag Class Component Renderer Encode Decode Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Life-cycle: Actors in the JSF process Life-cycle: Actors in the JSF process 13 14 Internationalization ? I18n in Java ◮ Multilingual web applications ◮ A Locale object contains i18n configurations • Work of programmers should be used anywhere in the world public Locale(String language) • Translation should not require any informatics knowledge public Locale(String lang, String country) public Locale(String lang, String ctry, String variant) ◮ Structure • Web application without any text, ◮ Resource bundles: • Data Base designed to handle multilingual texts, • Provide facilities for storage and retrieval of all locale-specific • Static texts are stored in resource bundles. information, independently from the application logic ◮ Language • Allow to support multiple locales in a single application • Automatically recognized from the browser, • Allow to extend internationalization easily • Comparison between the site and the browser, • The user can also change the desired language. ◮ The Java Resource bundles classes are: ◮ Priority • ResourceBundle contains locale-specific objects. • browser identification (lowest) • ListResourceBundle abstract subclass of ResourceBundle • Locale in the session • PropertyResourceBundle is a concrete subclass of • Change using an event (highest) ResourceBundle (property files). Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Advanced Web Technologies 6) JSF Lifecycle, Event handling, data binding, i18n Internationalization - I18n: Motivations Internationalization - I18n: I18n in Java (very short presentation) 15 16
Recommend
More recommend