在实际项目开发中,页面的导航(navigation)规则会比较复杂。整个系统的页面浏览风格先设计好很重要,点击按钮请求新页面,是在原browser window(view port)中刷新页面,还打开新的window;某个页面可能只允许打开一个window;是否使用dynamic tab等等。本文总结几点在ADF中请求页面的方法。当然ADF框架中的控制层可以使用JSF page flow和ADF task flow,ADF task flow具有更好的可重用性和模块化。这里只描述JSF page flow,ADF task flow的操作类似。
1,使用JSF page flow中的navigation rule
<navigation-rule>
<from-view-id>/untitled1.jsf</from-view-id>
<navigation-case>
<from-outcome>toPage2</from-outcome>
<to-view-id>/untitled2.jsf</to-view-id>
</navigation-case>
</navigation-rule>
1)在command button等具有Action属性的组件中使用该Rule:
<af:commandButton text="commandButton 1" id="cb1" action="toPage2"/>
2)在Java代码中可以返回“toPage2”来控制页面的导航
public String onClick() {
//在页面刷新前执行一些逻辑操作
if (xxx) {
return "toPage2";
} else {
return "toPage1";
}
}
3)在没有Action属性的组件中可以进行如下操作,比如点击CheckBox,true时导航到新页面,需要设置AutoSubmit为true
public void setSelected(boolean selected) {
this.selected = selected;
FacesContext fc = FacesContext.getCurrentInstance();
if (selected) {
fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "toPage2");
}
}
2,使用Java代码导航到任意的页面
1)如果没在JSF page flow中定义navigation rule,可以使用下面的代码来刷新到新的页面
FacesContext fctx = FacesContext.getCurrentInstance();
ViewHandler vh = fctx.getApplication().getViewHandler();
UIViewRoot viewToRender = vh.createView(fctx, "/untitled2.jsf");
fctx.setViewRoot(viewToRender);
fctx.renderResponse();
3,在新窗口(window)中打开页面的方法
1)使用af: goLink,destination指定为目标页面File Name,targetFrame为_blank
<af:goLink text="goLink 1" id="gl1" targetFrame="_blank" destination="untitled2.jsf"/>
2)使用redirect
可以参考我以前的文章:ADF页面重定向
3)这里提到的1)和2)两种方法,可以指向外部地址,做为一个连接到外站的方法
2011/12/17 卢玉双 @上海