在OperaMasks中可以用事件进行导航,简单的说就是当某个事件发生时页面会进行导航,这是正常的,也是必须的。我们来看一个例子:
有时候会有这样一种需求:程序员申请费用,根据费用的大小,要自动导航到不同的页面。但有时候,要重定向到哪些页面我们事先并不清楚。以上述场景为例:当项目经理审批时,希望导航到projectManager.xhtml页面,当部门经理审批时,希望导航到 departManager.xhtml页面。在OperaMasks 3.2中,如何解决此需求?
我们首先完成这两个页面:
projectManager.xhtml:
<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
xmlns:w="http://www.apusic.com/jsf/widget" xmlns:layout="http://www.apusic.com/jsf/layout"
renderKitId="AJAX" xmlns:h="http://java.sun.com/jsf/html">
<w:page title="project manager">
I'm your project manager, you apply <h:outputText id="money"/> money.
</w:page>
</f:view>departManager.xhtml
<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
xmlns:w="http://www.apusic.com/jsf/widget" xmlns:layout="http://www.apusic.com/jsf/layout"
renderKitId="AJAX" xmlns:h="http://java.sun.com/jsf/html">
<w:page title="project manager">
I'm your depart manager, you apply <h:outputText id="money"/> money.
</w:page>
</f:view>再对 ProjectManagerBean及DepartManagerBean修订如下:
@ManagedBean(name="projectManagerBean", scope=ManagedBeanScope.SESSION)
public class ProjectManagerBean {
@Bind
private int money;
@EventListener("applyLittleFee")
private String applyFeeListener(int money) {
this.money = money;
return "view:projectManager";
}
}@ManagedBean(name="DepartManagerBean", scope=ManagedBeanScope.SESSION)
public class DepartManagerBean {
@Bind
private int money;
@EventListener("applyMuchFee")
private String applyFeeListener(int money) {
this.money = money;
return "view:departManager";
}
}请注意观察:return "view:projectManager"语句中,前面的view意即告诉OperaMasks,这是要导航,后面的projectManager代表view的ID。
程序运行效果如下:
问题:如果一个事件有多个Listener,并且每个Listener都会返回“view:somePage",情况会如何?答案是:以第一个返回结果为准。