Download presentation
Presentation is loading. Please wait.
1
J2EE Lecture 4: Advanced Concepts in JSF
Dr. Ming Qiu Xiamen University Software School
2
4.1 Creating and initializing beans
JSF In Action, Chap 3 4.1 Creating and initializing beans Managed Bean Creation facility Declare all of your beans and initialize all of their properties in one central place. Control the scope (application, session, or request) where a bean is stored. Change a bean’s class or initial property values without changing any code. Initialize a bean property with value-binding expressions. Access a managed bean using ordinary JSF EL expressions.
3
4.1 Creating and initializing beans
JSF In Action, Chap 3 4.1 Creating and initializing beans Creating and initializing beans Initializing simple properties Initializing List and array properties Initializing Map properties Declaring Lists and Maps as managed beans Setting values with value-binding expressions
4
<managed-bean>
<managed-bean-name>brokeUser</managed-bean-name> <managed-bean-class>org.jia.examples.UserBean </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>firstName</property-name> <value>Joe</value> </managed-property> <property-name>lastName</property-name> <value>Broke</value> <property-name>balance</property-name> <value>0</value> <property-name>favoriteAnimal</property-name> <null-value/> </managed-bean> JSF In Action, Chap 3
5
<managed-bean>
JSF In Action, Chap 3 <managed-bean> <managed-bean-name>user</managed-bean-name> <managed-bean-class>org.jia.examples.UserBean </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> ... <managed-property> <property-name>favoriteSites</property-name> <list-entries> <value> <value> <value> <value> <value> <value> </list-entries> </managed-property> </managed-bean>
6
4.1 Creating and initializing beans
JSF In Action, Chap 3 4.1 Creating and initializing beans <managed-bean> ... <managed-property> <property-name>favoriteNumbers</property-name> <list-entries> <value-class>java.lang.Integer</value-class> <value>31415</value> <value>278</value> <value>304</value> <value>18</value> <value>811</value> <value>914</value> </list-entries> </managed-property> </managed-bean>
7
<managed-bean>
JSF In Action, Chap 3 <managed-bean> <managed-bean-name>user</managed-bean-name> <managed-bean-class>org.jia.examples.UserBean </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> ... <managed-property> <property-name>favoriteSitesMap</property-name> <map-entries> <map-entry> <key>JSF Central</key> <value> </map-entry> <key>TheServerSide.com</key> <value> </value>
8
<key>IBM DeveloperWorks</key>
<map-entry> <key>IBM DeveloperWorks</key> <value> </value> </map-entry> <key>Oracle Technology Network</key> <value> <key>java.net</key> <value> <key>Manning Publications</key> <value> </map-entries> ... </managed-property> JSF In Action, Chap 3
9
4.1 Creating and initializing beans
JSF In Action, Chap 3 4.1 Creating and initializing beans <managed-bean> <description>List of favorite sites.</description> <managed-bean-name>favoriteSites</managed-bean-name> <managed-bean-class>java.util.ArrayList</managed-bean-class> <managed-bean-scope>application</managed-bean-scope> <list-entries> <value> <value> <value> <value> <value> <value> </list-entries> </managed-bean>
10
4.1 Creating and initializing beans
JSF In Action, Chap 3 4.1 Creating and initializing beans
11
JSF In Action, Chap 3
12
4.1Creating and initializing beans
JSF In Action, Chap 3 4.1Creating and initializing beans A managed bean can’t reference an object with a shorter life span than the managed bean itself
13
4.1Creating and initializing beans
JSF In Action, Chap 3 4.1Creating and initializing beans a scope of none means that the bean is created only when something else references it
14
4.2 Navigating the sea of pages
JSF In Action, Chap 3 4.2 Navigating the sea of pages JSF supports navigating from one page to another. similar to Struts’ ActionForwards. The heart of the navigation system is the navigation handler The actual code that decides which page to load next operates in response to action events
15
4.2 Navigating the sea of pages
JSF In Action, Chap 3 4.2 Navigating the sea of pages
16
4.2 Navigating the sea of pages
JSF In Action, Chap 3 4.2 Navigating the sea of pages
17
4.2 Navigating the sea of pages
JSF In Action, Chap 3 4.2 Navigating the sea of pages If you want to make sure that an outcome was generated by a specific action method <navigation-case> <from-action>#{orderManager.placeOrder}</from-action> <from-outcome>success</from-outcome> <to-view-id>/ConfirmOrder.jsp</to-view-id> </navigation-case>
18
4.2 Navigating the sea of pages
JSF In Action, Chap 3 4.2 Navigating the sea of pages Set up rules for all pages or groups of pages. <navigation-rule> <from-view-id>*</from-view-id> <navigation-case> <from-outcome>login</from-outcome> <to-view-id>/login.jsp</to-view-id> </navigation-case> <from-outcome>logout</from-outcome> <to-view-id>/logout.jsp</to-view-id> </navigation-rule>
19
JSF In Action, Chap 11 4.3 From servlets to JSF JSF’s classes can be organized into four main categories Application access to supported locales, factory methods for UI components, access to the default message resource bundle. responsible for creating ValueBinding and MethodBinding instances, which can be used to evaluate JSF EL expressions in code.
20
4.3 From servlets to JSF Context Event handling UI components.
JSF In Action, Chap 11 4.3 From servlets to JSF Context provide access to current request data, a handle to the outside environment, and entry to objects in other categories. Event handling responsible for processing user gestures In servlets, the request is the event. In JSF, request parameters are translated into event objects, and you can write listeners for processing those events. UI components.
21
JSF In Action, Chap 11
22
JSF In Action, Chap 11 4.3 From servlets to JSF The JSF Application class serve two important purposes. Provides access to configured parameters, like the set of supported locales, and the application’s message bundle. Provides factory methods for creating ValueBindings, MethodBindings, UIComponents, Validators, Converters, and so on. String bundleName = app.getMessageBundle(); ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
23
JSF In Action, Chap 11 4.3 From servlets to JSF Deprecated Deprecated
24
4.3 From servlets to JSF FacesContext
JSF In Action, Chap 11 4.3 From servlets to JSF FacesContext has all the stuff you need to interact with the UI and the rest of the JSF environment. Retrieve a new Application instance from the FacesContext: FacesContext facesContext = FacesContext.getCurrentInstance(); Application app = facesContext.getApplication();
25
JSF In Action, Chap 11 4.3 From servlets to JSF
26
4.3 From servlets to JSF import javax.el.ExpressionFactory
import javax.el.ELContext import javax.el.ValueExpression ExpressionFactory elFactory = application.getExpressionFactory(); ELContext elContext = facesContext.getELContext(); ValueExpression userExp = elFactory.createValueExpression(elContext, “#{user}”, User.class); User user = (User) userExp.getValue(elContext); Package javax.el is in JavaServer Pages API Documentation
27
4.4 Event handling JSF is event driven,
JSF In Action, Chap 11 4.4 Event handling JSF is event driven, interaction with the user interface is represented as event objects that know what event occurred and which component sent it. This happens during the decoding the Apply Request Values phase saves you the trouble of reading request parameters to attempt to decipher what the user did.
29
4.4 Event handling Action listeners
JSF In Action, Chap 11 4.4 Event handling Action listeners implement the javax.faces.event.ActionListener public void processAction(ActionEvent event) throws AbortProcessingException public class Foo{ public void nextPage(ActionEvent actionEvent){ // go to next page }
30
4.4 Event handling Action methods public String myAction();
JSF In Action, Chap 11 4.4 Event handling Action methods The default ActionListener instance invokes action methods based on the action property of the UI component firing the event. sends the action method’s outcome to the navigation handler. If the action property of the source component is a literal value instead of a method-binding expression, that value is used as the outcome. The navigation handler uses the outcome to set the proper page for JSF to display next public String myAction();
31
4.4 Event handling Handling value-change events
JSF In Action, Chap 11 4.4 Event handling Handling value-change events When the local value of an input component changes and passes all validations, it fires an instance of the javax.faces.event.ValueChangeEvent. Occurs before any model objects have been updated, but after conversion and validation have taken place.
32
4.4 Event handling Value-change listeners
JSF In Action, Chap 11 4.4 Event handling Value-change listeners public void processValueChange(ValueChangeEvent event) throws AbortProcessingException; public class Foo{ ... public void valueChange(ValueChangeEvent event){ // logic here }
33
JSF In Action, Chap 11 4.5 Components revisited
35
4.6 Understanding JSF development approaches
JSF In Action, Chap 10 4.6 Understanding JSF development approaches The form-centric approach works well for small applications
36
4.6 Understanding JSF development approaches
JSF In Action, Chap 10 4.6 Understanding JSF development approaches The object-based approach is more suitable for larger system, which need to be maintained for a long time
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.