2012年3月24日星期六

ADF_113:使用Managed Bean访问各个Scope变量

开发环境:JDevloper 11.1.2.1.0+ Oracle Database 10g Express Edition 10.2.0.1。

开发中经常会遇到访问不同Scope变量的需求。本文就这一问题作了一个实验来说明如何使用Manged Bean访问。

1. 页面代码
在页面中,使用了6个InputText来保存ADF中不同Scope的变量:RequestScope、ViewScope、PageFlowScope、BackingBeanScope、SessionScope、ApplicationScope。

<af:form id="f1">
<af:inputText label="ADF RequestScope:" id="it1" value="#{requestScope.requestVar}"/>
<af:inputText label="ADF View Scope:" id="it2" value="#{viewScope.viewVar}"/>
<af:inputText label="ADF PageFlow Scope:" id="it3" value="#{pageFlowScope.pageFlowVar}"/>
<af:inputText label="ADF BackingBean Scope:" id="it4" value="#{backingBeanScope.backingBeanVar}"/>
<af:inputText label="ADF Session Scope: " id="it5" value="#{sessionScope.sessionVar}"/>
<af:inputText label="ADF Application Scope: " id="it6" value="#{applicationScope.applicationVar}"/>
<af:commandButton text="Submit" id="cb1" actionListener="#{myBackingBean.submitButton_action}"/>
</af:form>


2. Managed Bean代码

package view;

import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.share.ADFContext;

import org.apache.myfaces.trinidad.context.RequestContext;

import view.util.JSFUtils;

public class MyBackingBean {
public MyBackingBean() {
}

public void submitButton_action(ActionEvent evt) {
System.out.println("############## Get Scope Vaiables from ADFContext ################");
ADFContext ac = ADFContext.getCurrent();
Map requestScope = ac.getRequestScope();
Map viewScope = ac.getViewScope();
Map pageFlowScope = ac.getPageFlowScope();
//Map backingBeanScope = ac.getBackingBeanScope();
Map sessionScope = ac.getSessionScope();
Map applicationScope = ac.getApplicationScope();

String requestVar = (String)requestScope.get("requestVar");
System.out.println("############## requestVar: " + requestVar);
String viewVar = (String)viewScope.get("viewVar");
System.out.println("############## viewVar: " + viewVar);
String pageFlowVar = (String)pageFlowScope.get("pageFlowVar");
System.out.println("############## pageFlowVar: " + pageFlowVar);
String backBeanVar = (String)JSFUtils.getManagedBeanValue("backingBeanScope.backingBeanVar");
System.out.println("############## backBeanVar: " + backBeanVar);
String sessionVar = (String)sessionScope.get("sessionVar");
System.out.println("############## sessionVar: " + sessionVar);
String applicationVar = (String)applicationScope.get("applicationVar");
System.out.println("############## applicationVar: " + applicationVar);

//requestScope.clear();
//viewScope.clear();
//pageFlowScope.clear();
//sessionScope.clear();
//applicationScope.clear();

System.out.println("############## Get Scope Vaiables from RequestContext ################");
RequestContext rc = RequestContext.getCurrentInstance();
String pageFlowVar2 = (String)rc.getPageFlowScope().get("pageFlowVar");
System.out.println("############## pageFlowVar2: " + pageFlowVar2);

System.out.println("############## Get Scope Vaiables from FacesContext ################");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
Map requestMap = ec.getRequestMap();
Map sessionMap = ec.getSessionMap();
Map applicationMap = ec.getApplicationMap();

String initVar = ec.getInitParameter("administrator.email");
System.out.println("############## initVar: " + initVar);
String requestVar3 = (String)requestMap.get("requestVar");
System.out.println("############## requestVar3: " + requestVar3);
String sessionVar3 = (String)sessionMap.get("sessionVar");
System.out.println("############## sessionVar3: " + sessionVar3);
String applicationVar3 = (String)applicationMap.get("applicationVar");
System.out.println("############## applicationVar3: " + applicationVar3);
}
}

(1)使用ADFContext访问不同Scope中的变量
可以看出,除了BackingBeanScope之外,其它Scope的变量都提供了相应的API。输出结果如下:
############## Get Scope Vaiables from ADFContext ################
############## requestVar: 1
############## viewVar: 2
############## pageFlowVar: 3
############## backBeanVar: 4
############## sessionVar: 5
############## applicationVar: 6

(2)使用RequestContext访问PageFlowScope中的变量
个人感觉这种方式不太标准,因为我只找到访问PageFlowScope变量的API。输出结果如下:
############## Get Scope Vaiables from RequestContext ################
############## pageFlowVar2: 3

(3)使用FacesContext访问不同Scope中的变量
FacesContext是JSF标准的API,JSF不包括ViewScope、PageFlowScope和BackingBeanScope。
如果你开发纯JSF应用的话,就要使用FacesContext,而不能使用ADFContext。输出结果如下:
############## Get Scope Vaiables from FacesContext ################
############## initVar: admin@oracle.com
############## requestVar3: 1
############## sessionVar3: 5
############## applicationVar3: 6

另外,我还展示了如何获取应用的Init Parameter。
首先要在web.xml中,添加一个context-param定义,比如:
<context-param>
<param-name>administrator.email</param-name>
<param-value>admin@oracle.com</param-value>
</context-param>
然后使用getInitParameter("administrator.email")方法获取参数值。
注意,context-param是应用级的参数,与定义在Servlet或Filter中定义的init parameter不同。
后者是属于该Servlet或Filter的,因此要在相应的Servlet或Filter代码中获取。比如:
String value = getServletConfig().getInitParameter("param1");
String value = getServletContext().getInitParameter("param1");


Project 下载:ADF_GetScopeVariables.7z

参考文献:
1. http://biemond.blogspot.jp/2011/01/some-handy-code-for-your-managed-beans.html
2. http://biemond.blogspot.jp/2009/03/some-handy-code-for-backing-beans-adf.html

没有评论: