JSF Quickstart With MyEclipse---6
天下维客,你可以修改的网络知识库
|
JSF编辑 相关文档:
其他资源: |
Connecting Everything Together (faces-config.xml)
A JSF application's navigability is specified in a declarative manner in the faces-config.xml deployment descriptor, as shown in Figure 16. In this configuration, there is one rule defined for a single page, with two possible outcomes. The first outcome is a successful login, in which case we will be forwarded to userLoginSuccess.jsp. Another outcome is failure, in which case we will be sent back to our login page to try again.
(Navigation rules in faces-config.xml)
<navigation-rule>
<from-view-id>/userLogin.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/userLoginSuccess.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/userLogin.jsp</to-view-id>
</navigation-case>
</navigation-rule>
Figure 16 - Application Navigation Configuration
Please notice that the values between the from-outcome tags in Figure 16 are the strings that are returned from the UserBean.loginUser() method. By referencing the returned strings from that method, JSF can determine the locations that the client should be forwarded to. This is similar to the way ActionForwards work in Struts, but in a more declarative fashion.
Once the navigation rules are defined, the javabeans used by the forms can be configured, as shown in Figure 17.
(Managed beans in faces-config.xml)
<managed-bean>
<description>Bean used to handle all User-based operations.</description>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>com.jsfdemo.bean.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Figure 17 - Configuring the Managed Javabeans
For Struts users, this section will look familiar since it is reminiscent of defining an Action and its accompanying Form. Here you can see we give a description of our bean, a name, a class and a scope.
The completed faces-config.xml file is shown in Figure 18.
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "<a href="http://java.sun.com/dtd/web-facesconfig_1_1.dtd" class="external free" target="_blank" title="http://java.sun.com/dtd/web-facesconfig_1_1.dtd" rel="nofollow">http://java.sun.com/dtd/web-facesconfig_1_1.dtd</a>">
<faces-config>
<navigation-rule>
<from-view-id>/userLogin.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/userLoginSuccess.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/userLogin.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<description>Bean used to handle all User-based operations.</description>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>com.jsfdemo.bean.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Figure 18 - Completed faces-config.xml Deployment Descriptor
At this point, the application is complete and ready for testing, which will be done in Section 9.


