Let me explain an interesting use case on 'file downloading' and a possible solution at this time. Requirement is to accept user input and validate the same, before initiating the file download. If the validation fails, then file download should get suspended.
ADF Faces recommends to use <af:fileDownloadActionListener> for downloading file. This provides a declarative solution for the problem, in most of the cases. As you can see from tag documentation, <af:fileDownloadActionListener> is designed in a very generic way. Currently we don’t have any direct mechanism to intercept the invocation of fileDownloadActionListener, apparently solution for the above mentioned use case turns out to be bit tricky. (Thanks to Jeanne Waldman for sharing this idea )
Interesting part of the solutions is queuing the ActionEvent for a ‘hidden’ action component which wraps fileDownloadActionListener. The java script method to queue ActionEvent is invoked from second button’s (this appears as download button for the end user) actionListener method, conditionally (if all business criteria’s are met). Let me detail the steps below.
Solution: To keep the logic simple, let us define 2 buttons, one hidden which has the fileDownloadActionListener tag inside. Second button (Download button) actually perform custom business process (like validating user input) before download starts. Please remember that, our objective is to validate the user input first and then download the file. If validation fails, suspend file download. So Download button performs required business checks (validates user inputs) and if all goes well, calls client side java script method to generate ‘AdfActionEvent‘ for the hidden button that in turn triggers file download.
java script method to queue AdfActionEvent for the hidden button
The actionListener method for the Download button:
Unzip the project and run the test.jspx. Click on Download button, key in the file name and press OK. You can observe that, file download starts only after validating the input. If validation fails, file download is getting suspended.
[Runs with Oracle JDeveloper 11g R1 PS1]
Learn More ...
There are a lot more points like this. If you are curious to learn the internals of the ADF Business Components and ADF Binding Layer, the following book is for you - Oracle ADF Real World Developer’s Guide.
More details about this book can be found in this post- http://jobinesh.blogspot.in/2012/07/pre-order-your-copy-of-oracle-adf-real.html
ADF Faces recommends to use <af:fileDownloadActionListener> for downloading file. This provides a declarative solution for the problem, in most of the cases. As you can see from tag documentation, <af:fileDownloadActionListener> is designed in a very generic way. Currently we don’t have any direct mechanism to intercept the invocation of fileDownloadActionListener, apparently solution for the above mentioned use case turns out to be bit tricky. (Thanks to Jeanne Waldman for sharing this idea )
Interesting part of the solutions is queuing the ActionEvent for a ‘hidden’ action component which wraps fileDownloadActionListener. The java script method to queue ActionEvent is invoked from second button’s (this appears as download button for the end user) actionListener method, conditionally (if all business criteria’s are met). Let me detail the steps below.
Solution: To keep the logic simple, let us define 2 buttons, one hidden which has the fileDownloadActionListener tag inside. Second button (Download button) actually perform custom business process (like validating user input) before download starts. Please remember that, our objective is to validate the user input first and then download the file. If validation fails, suspend file download. So Download button performs required business checks (validates user inputs) and if all goes well, calls client side java script method to generate ‘AdfActionEvent‘ for the hidden button that in turn triggers file download.
<af:commandButton text="HiddenBtn" id="HiddenBtn"
clientComponent="true" visible="false"
partialSubmit="false">
<af:fileDownloadActionListener filename="#{TestBean.fileName}"
method="#{TestBean.downloadMe}"
contentType="text/plain; charset=utf-8"/>
</af:commandButton>
<af:commandButton text="Download" id="dwnloadBtn"
actionListener="#{TestBean.prepareForDownloadAction}"
clientComponent="true"
partialSubmit="true"/>
java script method to queue AdfActionEvent for the hidden button
function customHandler(event) {
hidePopup(event);
var exportCmd = AdfPage.PAGE.findComponentByAbsoluteId("HiddenBtn");
var actionEvent = new AdfActionEvent(exportCmd);
actionEvent.forceFullSubmit();
actionEvent.noResponseExpected();
actionEvent.queue();
}
The actionListener method for the Download button:
public void prepareForDownloadAction(ActionEvent act) {
/**
* You can add your business logic here to process the
* user input, and based on the outcome you can decide
* whether to continue the processing or not.
*/
FacesContext context = FacesContext.getCurrentInstance();
ExtendedRenderKitService erks =
Service.getService(context.getRenderKit(),
ExtendedRenderKitService.class);
erks.addScript(context, "customHandler();");
}
You can download the sample workspace from here. Unzip the project and run the test.jspx. Click on Download button, key in the file name and press OK. You can observe that, file download starts only after validating the input. If validation fails, file download is getting suspended.
[Runs with Oracle JDeveloper 11g R1 PS1]
Learn More ...
There are a lot more points like this. If you are curious to learn the internals of the ADF Business Components and ADF Binding Layer, the following book is for you - Oracle ADF Real World Developer’s Guide.
More details about this book can be found in this post- http://jobinesh.blogspot.in/2012/07/pre-order-your-copy-of-oracle-adf-real.html