Showing posts with label ADF Code Examples. Show all posts
Showing posts with label ADF Code Examples. Show all posts

Friday, June 27, 2014

ADF Code Examples: af:selectOneChoice


Oracle 11gThis component generates a menu link with a single menu selection model. The value that goes to backing bean by default is an index (0,1,2) unless the valuePassThru is true. If valuePassThru is false then the selection value is based on indices. I am trying to use as much properties in the examples but please free to use them selectively.



Populating af:selectOneChoice  using af:forEach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<af:selectOneChoice id="sc1" clientComponent="true"
  required="false" immediate="true" 
  shortDesc="#{bundle.TYPES}"
  showRequired="true" mode="default" 
  contentStyle="width:30px" 
  binding="#{pageFlowScope.myBean.types}"
  value="#{pageFlowScope.myBean.type}"
  unselectedLabel="#{bundle.SELECT_ONE}"
  label="#{sprportalBundle.TYPES}:"
  autoSubmit="false">
 <af:forEach items="#{bindings.typeLOV.iteratorBinding.allRowsInRange}"
   var="item">
  <f:selectItem id="sit1" itemLabel="#{item.type}"
    itemValue="#{item.type}"/>
 </af:forEach>
</af:selectOneChoice>


Populating af:selectOneChoice  using individual af:selectItem tags

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<af:selectOneChoice label="#{bundle.MYLABEL}"
  id="xs1" required="false"
  immediate="true"
  valueChangeListener="#{pageFlowScope.myBean.typesVCL}"
  showRequired="true"
  value="#{pageFlowScope.myBean.type}"
  binding="#{pageFlowScope.myBean.typeBinding}"
  styleClass="classname">
 <af:selectItem label="#{bundle.CHOICE_TYPE1}"
   value="#{pageFlowScope.myBean.typeValue1}"
   id="xs2"/>
 <af:selectItem label="#{bundle.CHOICE_TYPE1}"
   value="#{pageFlowScope.myBean.typeValue2}"
   id="xs3"
   rendered="#{pageFlowScope.myBean.typeValueShown}"/>
</af:selectOneChoice>

Populating af:selectOneChoice  using af:selectItems tag


1
2
3
4
5
6
7
8
 <af:selectOneChoice value="#{bindings.Team.inputValue}"  label="#{bundle.MYLABEL}"                      
  shortDesc="#{bindings.type.hints.tooltip}" id="xc2"
  binding="#{pageFlowScope.myBean.team}" autoSubmit="true"
  unselectedLabel="#{bundle.MYLABEL}"
  rendered="#{not pageFlowScope.myBean.noTeams}"
  simple="true">
 <f:selectItems value="#{bindings.Team.items}" id="xc3"/>
</af:selectOneChoice>


ADF Code Examples: af:commandButton [deprecated]


Oracle 11gThis post provides different flavors of af:commandButton usage however the component itself has been deprecated in favor of af:button (super button) in 12c. A command button generates an action event  on the server following a button press which largely differs from the behavior of a goButton.



Using af:commandButton to call an action method with params in your bean


1
2
3
4
5
6
7
8
9
<af:commandButton text="#{bundle.MY_BUTTON}"
  id="cl1"
  actionListener="#{myBean.actionMe}"
  partialSubmit="true"
  partialTriggers="cl2"
  rendered="#{myBean.showLink}">
 <af:setActionListener from="#{myBean.oneVariable}"
   to="#{myBean.otherVariable}"/>
</af:commandButton>


Using af:commandButton to open a popup


1
2
3
4
5
<af:commandButton text="#{bundle.MY_BUTTON}" id="cl1"
  partialSubmit="true"
  clientComponent="true">
 <af:showPopupBehavior triggerType="click" popupId="p1"/>
</af:commandButton>

Calling a JavaScript function with af:commandButton


1
2
3
4
5
6
7
8
9
 <af:commandButton text="#{bundle.MY_BUTTON}"
  id="cl1" 
  clientComponent="true"
  partialSubmit="true">
 <af:clientAttribute name="name"
   value="#{myBean.name}"/>
 <af:clientListener method="showAlert"
   type="click"/>
</af:commandButton>


Wednesday, June 25, 2014

ADF Code Examples: af:commandLink [Deprecated]


Oracle 11gThis post provides different flavors of af:commandLink usage however the component itself has been deprecated in favor of af:link in 12c. A command link generates an action event  on the server following a link click which largely differs from the behavior of a goLink.

Using af:commandLink to call an action method with params in your bean


1
2
3
4
5
6
7
8
9
<af:commandLink text="#{bundle.MY_LINK}"
  id="cl1"
  actionListener="#{myBean.actionMe}"
  partialSubmit="true"
  partialTriggers="cl2"
  rendered="#{myBean.showLink}">
 <af:setActionListener from="#{myBean.oneVariable}"
   to="#{myBean.otherVariable}"/>
</af:commandLink>


Using af:commandLink to open a popup


1
2
3
4
5
<af:commandLink text="#{bundle.SUI_RECENT_SEARCHES}" id="cl1"
  partialSubmit="true"
  clientComponent="true">
 <af:showPopupBehavior triggerType="click" popupId="p1"/>
</af:commandLink>

Calling a JavaScript function with af:commandLink


1
2
3
4
5
6
7
8
9
 <af:commandLink text="#{row.header}"
  id="cl1" 
  clientComponent="true"
  partialSubmit="true">
 <af:clientAttribute name="name"
   value="#{myBean.name}"/>
 <af:clientListener method="showAlert"
   type="click"/>
</af:commandLink>



Top adf code snippets for common tasks

Hello there!  Today let’s see the top commonly used ADF snippets (at least that’s what I thought while starting to share itso here you go with the entries.


1.      Getting the page bindings


This is commonly used to get the page bindings in the managed bean to get either specific binding values or get a hold on the action for the binding.

1:    public static BindingContainer getBindingContainer() {  
2:      FacesContext facesContext = FacesContext.getCurrentInstance();  
3:      Application app = facesContext.getApplication();  
4:      ExpressionFactory elFactory = app.getExpressionFactory();  
5:      ELContext elContext = facesContext.getELContext();  
6:      ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings}", Object.class);  
7:      return (BindingContainer)valueExp.getValue(elContext);  
8:    }  

2.     Getting hold off the Application Module


Application modules are required to do any sort of tasks binding the view object and the view links and all wrapped around by services. So needless to say that whenever there is a need to call your service getting to your AM is your first step.


1:    public static ApplicationModule getApplicationModuleForDataControl(String name) {  
2:      BindingContext bindingContext = BindingContext.getCurrent();  
3:      ApplicationModule appModule = null;  
4:      if (null != bindingContext) {  
5:        DCDataControl dc = bindingContext.findDataControl(name);  
6:        if (dc != null) {  
7:          appModule = (ApplicationModule)dc.getDataProvider();  
8:        }  
9:      }  
10:      return appModule;  
11:    }  


3.     Resource Bundle Properties


     There are scenarios where we want to get the property values from the resource bundle in our beans and this happens more often than you think!

1:    public static String getResourceProperty(String label, Locale userLocale) {  
2:      String bundle_name = "com.example.mybundle";  
3:      String txt = null;  
4:      try {  
5:        ResourceBundle bundle = BundleFactory.getBundle(bundle_name, userLocale);  
6:        if (bundle != null) {  
7:          txt = bundle.getString(label);  
8:        }  
9:      } catch (Exception e) {  
10:        logger.severe("Problem in loading resource bundle: " + bundle_name, e.getMessage());  
11:      }  
12:      return txt;  
13:    }  

4.     Is the request a PPR?


     Ever wondered if a request is a normal full request or a partial page request, well the following snippet gives you that information. This is a very useful thing to know when you try to filter out requests in your listeners.

1:    public static boolean isPprRequest() {  
2:      FacesContext facesContext = FacesContext.getCurrentInstance();  
3:      return AdfFacesContext.getCurrentInstance().isPartialRequest(facesContext);  
4:    }  

5.     Retrieving the request headers from within managed bean


     We can get the request headers from the bean using the following snippet


1:  FacesContext facesContext = FacesContext.getCurrentInstance();  
2:  ExternalContext externalContext = facesContext.getExternalContext();  
3:  Map requestHeaderMap = externalContext.getRequestHeaderMap();  


6.     Accessing request and response objects from a bean




1:  FacesContext facesContext = FacesContext.getCurrentInstance();  
2:  ExternalContext externalContext = facesContext.getExternalContext();  
3:  HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();  
4:  HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest(); 


7.     Putting and retrieving from session scope


     Quick ways to manage objects in session scope.


1:  ADFContext.getCurrent().getSessionScope().put("abc", abcValue);  
2:  ADFContext.getCurrent().getSessionScope().remove("abc");  
3:  ADFContext.getCurrent().getSessionScope().get("abc");  

8.     Adding partial targets at runtime



     We could add partial targets to components at runtime so that the property changes made in a bean method to a previously non target component reflects in UI.

1:  AdfFacesContext.getCurrentInstance().addPartialTarget(myComponent); 


9.     Adding JavaScript to response



     More often than not we might be required to trigger some external Javascript on click of a button. To achieve it we use the following code.

1:    public static void addScriptOnPartialRequest(String script) {  
2:      logger.fine("Adding Java Script " + script);  
3:      FacesContext context = FacesContext.getCurrentInstance();  
4:      if (AdfFacesContext.getCurrentInstance().isPartialRequest(context)) {  
5:        ExtendedRenderKitService erks = Service.getRenderKitService(context, ExtendedRenderKitService.class);  
6:        erks.addScript(context, script);  
7:      }  
8:    }  


10. Get the current Locale


     Snippet to get the current locale


1:  public static Locale getLocale() {  
2:  ADFContext adfctx = ADFContext.getCurrent();  
3:  return adfctx.getLocale();  
4:  }