Showing posts with label Backing Bean. Show all posts
Showing posts with label Backing Bean. Show all posts

Saturday, June 12, 2010

Bit #19 - Downloading a file

Use an af:fileDownloadActionListener Rich Faces component to download data from the server to a client machine. Add the listener inside the component that will initiate the download, an af:commandButton for example, and specify the content type and the name of the file. Also specify a method in a backing bean that will be called to perform the download. In the backing bean method get the formated data to be saved from the model and use the supplied OutputStream to write the data.


Example:

In .jsf page:

<af:commandbutton id="buttonid"  " text="Download" ...
    <af:filedownloadactionlistener contenttype="application/octet-stream"
                method="#{backingBean.doDownload}"  filename="defaultFilename.txt"/>          
</af:commandButton>


In backing bean:

  public void doDownload(FacesContext facesContext, OutputStream outputStream) {
      // write the neccessary code to get the download data from the Model
      String data = getDownloadData();

      // save to the output stream
      try {
          OutputStreamWriter writer = new OutputStreamWriter(outputStream,"UTF-8");
           writer.write(data);
           writer.close();
          outputStream.close();
      } catch (IOException e) {
          // handle I/O exceptions
      }
  }


Context:

JSF Page
Backing Bean

Sunday, April 18, 2010

Bit #12 - Accessing the authenticated user's security roles from a backing bean

To access the authenticated user's security roles from a backing bean, first retrieve the SecurityContext from the current ADFContext instance and then call its getUserRoles() method. getUserRoles() returns a String array of all the roles defined for the user. To determine whether a specific role is assigned to the user, call the  SecurityContext  isUserInRole() method specifying the role as an argument. This method will return a boolean indicator of whether the role is assigned to the user or not.


Example:

    // in the context of a backing bean

    public String[] getUserRoles(){
        return ADFContext.getCurrent().getSecurityContext().getUserRoles();
    }

    public boolean isUserInRole(){
        return ADFContext.getCurrent().getSecurityContext().isUserInRole("RoleName");
    }


Context:

Backing Bean


Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Share, getUserRoles

Saturday, April 17, 2010

Bit #11 - Accessing an object stored at the PageFlowScope from a backing bean

You can access the PageFlowScope from a backing bean, by getting the AdfFacesContext instance and calling its getPageFlowScope() method. This will return the Map of all objects stored in the PageFlowScope. To retrieve a specific object, call get() on the Map specifying the object identifier. Similarly, call the getViewScope() and getProcessScope() methods of the AdfFacesContext to retrieve the ViewScope and ProcessScope respectively.

Example:

        // in the context of a backing bean
        Object data = AdfFacesContext.getCurrentInstance().getPageFlowScope().get("objectID");


Context:

Backing Bean


Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Faces, getPageFlowScope

Thursday, April 15, 2010

Bit #10 - Selectively enabling Partial Page Rendering programmatically

You can selectively enable Partial Page Rendering (PPR) programmatically from a backing bean, by calling the AdfFacesContext method addPartialTarget(). To do so, simply bind the target component to the backing bean, get the AdfFacesContext  and call its addPartialTarget() method passing the bound component as a parameter. This will in effect rerender the component. The advantage of calling addPartialTarget() is that it will rerender the component selectively for the specific events that you choose.

Example:

    // in the context of the backing bean

    // bind the component to the backing bean
    private RichPanelBox panel;

    public void setPanel(RichPanelBox panel) {
        this.panel = panel;
    }

    public RichPanelBox getPanel() {
        return panel;
    }

    // rerender the component
    private void rerenderComponent() {
        AdfFacesContext.getCurrentInstance().addPartialTarget(this.panel);
    }

Context:

Backing Bean

Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Faces, addPartialTarget

Friday, April 9, 2010

Bit #8 - Executing an operation binding programmatically from a backing bean

To access an OperationBinding in a backing bean, call getOperationBinding() on the DCBindingContainer - the data control binding container - and specify the operation binding identifier that you assigned to the operation during the declarative binding process. Once you have the OperationBinding call its execute() method to execute it. If you need to pass any arguments to the operation, call getParamsMap() to get the operation parameters map and put() on the map the specific argument. Call getResult() after the call to execute() to retrieve the result of the execution. Here is an example.


Example:

        // in the context of a backing bean

         OperationBinding operation = bindings.getOperationBinding("operation_name");

         operation.getParamsMap().put("parameter_name", parameterValue);

         operation.execute();

         if (operation.getResult() != null) {
                  Boolean result = (Boolean) operation.getResult(); // cast to the expected result type
        }


Context:

Backing Bean


Tuesday, April 6, 2010

Bit #5 - Accessing a resource bundle from a backing bean

You can access a resource bundle from a backing by calling the getResourceBundle() method on a javax.faces.application.Application object and specifying the bundle name. You get the Application object by calling getApplication() on the FacesContext. The getResourceBundle() returns a ResourceBundle object. To get some resource data from the bundle, call getString() on the ResourceBundle specifying the resource identifier. The example below illustrates the case.

Example:


        FacesContext fc = FacesContext.getCurrentInstance();
        ResourceBundle bundle = fc.getApplication().getResourceBundle(fc,"bundle_name");
        bundle.getString("resource_identifier");


Context:

Resource Bundle
Backing Bean


Related Posts Plugin for WordPress, Blogger...