JAVA SERVER FACES APPLICATION CONFIGURATION
JSF technology provides a configuration file known as faces-config.xml. This file allows defining rules for navigation, initializing, JavaBeans, registering its own custom JSF components and validators and configuring several other aspect of a JSF application.
A Sample faces-config.xml file :
1. <?xml version = ‘1.0’ encoding = ‘UTF-8’?>
2. <faces-config version = “2.0”
3. xmlns = http://java.sun.com/xml/ns/javaee
4. xmlns = xsi = http://www.w3.org/2001/XMLSchema-instance”
5. xsi : schemaLocation = “http://java.sun.com/xml/ns/javae
http : // java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd”>
6. <navigation-rule>
7. <from-view-id>/index.xhtml</from-view-id>
8. <navigation-case>
9. <from-outcome>login</from-outcome>
10. <to-view-id>/welcome.xhtml</to-view-id>
11. </navigation-case>
12. <navigation-rule>
13. <managed-bean>
14. <managed-bean-name> user</managed-bean-name>
15. <managed-bean-class>UserBean</managed-bean-class>
16. <managed-bean-scope>session</managed-bean-scope>
17. <managed-bean>
18. </faces-config>
Explanation:
The following are the elements available in the faces-config.xml file:
<?xml version – ‘1.0’ encoding = ‘UTF-8’?>
Is a valid XML version number and the character encoding part.
<faces-config> Element
<faces-config version =”2.0”
xmlns = http://java.sun.com/xml/ns/javaee
xmlns:xsi =http://www.w3/org/2001/XMLSchema -instance
xsi : schemaLocation = “http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd”>
The configuration information is a hierarchy.
The <faces-config> element is the root element which contains nested elements of all the other configuration settings.
The <navigation-rule> Element :
<navigation-rule>
………
</navigation-rule>
The <navigation-rule> element represents an individual decision rule. This rule is accessed when the default Navigation Handler is implemented. This rule helps make decisions on what view to display next based on the View id being processed.
The <from-view-id>Element
<from-view-id>/index.xhtml</from-view-id>
The <from-view-id> element contains the view identifier bound to the view for which the containing navigation rule is relevant. If no <from-view> element is specified. Then this rule automatically applies to navigation decisions on all views.
Since this element is not specified, a rule of “*” is assumed, which means that this navigation rule applies to all views.
The <navigation-case> Element
<navigation-case>
……..
</navigation-case>
The <navigation-case> element describes a specific combination of conditions that must match, for this case to be executed. It also describes the view id of the component tree that should be selected next.
The <from-outcome> Element
<from-outcome>login</from-outcome>
The <from-outcome> element contains a logical outcome string. This string is returned by the execution of an application action method selected via an actionRef property [ or by a literal value specified by an action property ] of a UICommand component.
If this element is specified, then this rule is relevant only if the outcome value matches this element’s value.
If this element is not specified, then this rule is relevant no matter what the outcome value was.
The <to-view-id> Element
<to-view-id>/welcome.xhtml</to-view-id>
The <to-view-id> element contains the identifier of the view that must be displayed when this navigation rule is matched.
The <managed-bean> Element :
<managed-bean>
……………
<managed-bean>
The <managed-bean> element represents a JavaBean of a specific class. This bean is dynamically instantiated at runtime. Especially, if it is referenced as the first element of a value reference expression and no corresponding bean can be identified in any scope.
In addition to the creation of the managed bean and the optional storing of it into the specified scope, nested <managed-property> elements can be used to initialize the contents of settable JavaBeans properties of the instance spawned.
The <managed-bean-name> Element
<managed-bean-name> user</managed-bean-name>
The <managed-bean-name> element represents the attribute name under which a managed bean is searched for as well as stored [unless the <managed-bean-scope> element’s value is none.]
The <managed-bean Class> Element
<managed-bean-class>UserBean</managed-bean-class>
The <managed-bean-class> element represents the fully qualified class name of the Java class that is used to instantiate a new instance of the bean, if creation of the specified managed bean is requested.
The value of the element must be of type ClassName. The specified class must conform to standard JavaBeans conventions.
In particular, it must have a public zero-arguments constructor and zero or more public property setters.
The <managed-bean-scope> Element
<managed-bean-scope> session <<managed-bean-scope>
The <managed-bean-scope> Element represents the scope into which a newly created instance of the specified managed bean is stored [unless the value is none.