用过seam的人都知道seam中支持action如果返回形如‘/xxx.xhtml’的导航字符串的时候会直接转到这个页面,而不管faces-config.xml中是否有这样的一个导航规则,这个对JSF的小改动对那些页面是动态选择的情况非常适用,比如你的页面地址是从数据库中获取的。
其实在JSF中实现这个小特性是非常简单的,只需要扩展一下JSF提供的默认导航处理器就可以了。
java 代码
- /**
- * 扩展JSF默认导航处理器,支持形式为'/index.faces'的outcome,直接定向到这个页面
- *
- * @author jones
- *
- */
- public class MyNavigationHandler extends NavigationHandlerImpl {
- @Override
- public void handleNavigation(FacesContext context, String fromAction,
- String outcome) {
- if(outcome != null && outcome.startsWith("/")){
- Application application = context.getApplication();
- ViewHandler viewHandler = application.getViewHandler();
- UIViewRoot newRoot = viewHandler.createView(context,outcome);
- context.setViewRoot(newRoot);
- }else{
- super.handleNavigation(context, fromAction, outcome);
- }
- }
- }