导航规则
导航规则是向导组件的核心。这些规则必须包含在应用程序的 faces-config.xml 文件中。下面是演示应用程序规则的一个子集。您可以使用这些规则作为起点来在 您自己的应用程序中利用向导组件。
< navigation-rule>
< from-view-id>/main.jsp< /from-view-id>
< navigation-case>
< description> If the action returns "next", goto firstSelection.jsp< /description>
< from-outcome>next< /from-outcome>
< to-view-id>/firstSelection.jsp< /to-view-id>
< /navigation-case>
< /navigation-rule>
< navigation-rule>
< from-view-id>/firstSelection.jsp< /from-view-id>
< navigation-case>
< description> If the action returns "next", goto secondSelection.jsp < /description>
< from-outcome>next< /from-outcome>
< to-view-id>/secondSelection.jsp< /to-view-id>
< /navigation-case>
< navigation-case>
< description> If the action returns "back", goto main.jsp < /description>
< from-outcome>back< /from-outcome>
< to-view-id>/main.jsp< /to-view-id>
< /navigation-case>
< /navigation-rule>
导航规则描述了如何根据动作的调用位置来处理动作,如下所示:
如果动作在 “"main.jsp” 页面调用并返回 “next”,则导航处理程序呈现 “firstSelection.jsp” 页面
如果动作在 “firstSelection.jsp” 页面调用并且返回“back”,则我们转到主页面
如果调用的动作返回 “next”,我们将转到 “secondSelection.jsp” 页面
动作
您可以回想到在工作流程图中首页面是特殊的,因为工作流程可以使得该页面重新加载。当用户希望向字符表中添加字符时就会重新加载首页面。这一添加是通过在 页面中放置一个按钮 来实现的,在按钮被按下时引发相应的动作。
在 “main.jsp” 页面中,您可以看到该按钮的声明:
< h:commandButton actionListener="#{modelBean.addCustomName}" value="Add Name"/>
实际的动作处理程序的实现在 ModelBean.java 文件中。“addCustomName”方法将 name 添加到表格中。
public void addCustomName(ActionEvent event) throws AbortProcessingException {
if ((customName != null) && (!customName.trim().equals(""))) { customName = customName.trim();
//check to see if name already exists in list
Iterator iter = dataList.iterator();
while (iter.hasNext()) {
CharacterBean item = (CharacterBean) iter.next();
if (item.getName().equals(customName)) {
reset();
return;
}
}
//create new entry
CharacterBean item = new CharacterBean();
item.setName(customName);
item.setSpecies((SpeciesBean)
speciesPropertyMap.get(customSpecies));
dataList.add(item);
}
}
当调用带有动作监听器的 CommandButton 时,就会调用动作程序。“addCustomName”方法遍历已有字符的列表,如果没有在列表中发现新的名字,它就创建一个新的 Character 条目。
如果这个动作处理程序有一个返回值,导航处理程序可以用该值来决定下一个动作并根据结果来决定导航的方向。在这个特殊的例子中没有返回值,因为只是重新显示同一个页面,但是添加了一个新的用户名条目。动作 处理程序和导航处理程序的设计决定了它们可以轻松地互操作。