在VO中引用Controller层中的Session State的两种方法

来源:互联网 发布:大地彩票v6 源码 编辑:程序博客网 时间:2024/05/19 00:52

http://blogs.oracle.com/smuenchadf/resource/examples中的第154个Example,Steven介绍了两种将Controller层中的值绑定给View Object中的变量的方法,其中的Session State可以理解为对应的一个Session值。

1,在adf.userSession.userData中保存Controller层传递过来的值,引用值的Groovy表达式为:adf.userSession.userData.FlavoriteColor。Steven推荐使用这种方法。

1)在Application Module中定义一个变量的Get/Set方法

    public String getSessionFavoriteColor() {      return (String)getDBTransaction().getSession().getUserData().get(FAVORITE_COLOR);            }    public void setSessionFavoriteColor(String color) {        String previousFavoriteColor = getSessionFavoriteColor();        getDBTransaction().getSession().getUserData().put(FAVORITE_COLOR,color);        if (valueChanged(previousFavoriteColor,color)) {            getExampleVO().executeQuery();        }    }
2)AM中expose该Java方法


3)通过Action绑定在Managed Bean中调用

    public void onFavoriteColorChanged(ValueChangeEvent valueChangeEvent) {      EL.setValueChangeEventComponentToNewValue(valueChangeEvent);      invokeOperationBinding("setSessionFavoriteColor");      invokeOperationBinding("Execute1");    }
这种处理方法要求在创建新的AM instance的时候,要调用setSessionFavoriteColor给一个新值,否则favoriteColor只能取一个默认值。

2,在adf.context.securityContext中保存Controller层的值,引用值的Groovy表达式为:adf.context.sessionScope.UserInfo.favoriteColor。

1)定义Session Scope的managed bean

2)给Data Control定义新的Factory Class,该Factory Class创建一个CustomDCJboDataControl Factory Instance。在页面Request的时候,将Session中的值传递到AM中

    public void beginRequest(HashMap requestCtx) {      String favoriteColor = EL.getAsString("#{UserInfo.favoriteColor}");      super.beginRequest(requestCtx);      if (favoriteColor != null) {        ExampleModule am = (ExampleModule)getDataProvider();        am.setSessionFavoriteColor(favoriteColor);      }    }

这种处理方法只保证每次创建AM instance的时候都取Session中保存的UserInfo对应的值。


State管理:

以上两种方法都涉及到AM的State管理,在AM钝化/激活的时候,处理AM中Controller层传递过来的值,AM State management相关的知识可以参考:Fusion Developer's Guide for Oracle Application Development:Application State Mangement

    @Override    protected void passivateState(Document document, Element element) {        System.out.println("passivate state: " + getSessionUserInfo());        element.setAttribute(USER_INFO, getSessionUserInfo());        super.passivateState(document, element);    }    @Override    protected void activateState(Element element) {        System.out.println("active state: " + element.getAttribute(USER_INFO));        super.activateState(element);        setSessionUserInfo(element.getAttribute(USER_INFO));    }

拓展

在使用ADF Security的情况下,在Model层中可以使用 adf.context.securityContext来得到Login用户的信息。ADF Security的灵活性有限,不能动态的创建角色以及角色相关联的页面信息。这时,我们会开发用户管理功能,自定义的Login页面,在用户login后,用户信息可以保存在HttpSession中或Session Scope的managed bean中,如何传递到Model层,就可以使用上面介绍的两种方法。




原创粉丝点击