4

I'm using JSF 2.0, Primefaces 3.4.2, Spring 3.1.2.

I'm facing a similar problem of the guy of this link: h:commandButton works from the second click.

Like him I'm not using ajax in the <p:commandButton> but I'm using a <p:fileDownload /> inside the button tag.

I have two views: "list.xhtml" and "downloadView.xhtml". In myBean.java I send a DataModel from view "list.xhtml" to view "downloadView.xhtml" as a request attribute as shown in the code below:

FacesUtil.getServletContext().setAttribute("myDataModelFromRequest", this.myDataModel);

The bean is anotted with @Scope("view")

In view "downloadView.xhtml" I populated succesfully a dataTable with the DataModel sent from request. But the problem happens when I click in the button to download the file. It only works on second try. I already tried to change the return of my method from null to "downloadView" but the problem was not solved.

In debug mode I noticed that only enter in the "downloadMethod()" on second click.

Anyone have an idea to solve it?


myBean.java

public String viewListMethod() {
    //some work here...

    FacesUtil.getServletContext().setAttribute("myDataModelFromRequest", this.myDataModel);

    return "downloadView";
}

downloadView.xhtml

<h:form id="formId" prependId="false">
    <p:dataTable
        id="dataTableId" var="myVar" value="#{myDataModelFromRequest}"                  
        selection="#{cargaProcessoControlador.myArray}" 
        paginator="true" rows="10" paginatorPosition="bottom" paginatorAlwaysVisible="false">
            <f:facet name="header">  
                    bla bla bla
            </f:facet>

            <p:column selectionMode="multiple" style="width:18px" />

            //collumns here...                      
    </p:dataTable>


    <p:commandButton id="btDownload" ajax="false" value="Download" icon="ui-icon-document" >
        <p:fileDownload value="#{myBean.downloadMethod()}" />
    </p:commandButton>
</h:form>
Community
  • 1
  • 1
romerompb
  • 860
  • 11
  • 15
  • 1
    The problem symptoms however suggests that you're navigating by ajax. Is this true? To exclude one and other, try `return "downloadView?faces-redirect=true";`. By the way, the servlet context does not represent the request scope, but the application scope. You're thus effectively setting/overriding the data for **all** application users! This is not the cause of your concrete problem, but I strongly recommend to fix and limit it to at most the session scope, or you'll run into major trouble during production. – BalusC Dec 14 '12 at 13:03
  • It could also be cause by the `myDataModelFromRequest` changing between the first request (when the page is loaded) and the second (when you click on the button). Not sure of the name of the phase but it doesn't like when object are changed at some point and just stop. I guess @BalusC knows the details around that one. (remember reading a post from you about it) – grekier Dec 14 '12 at 14:40

2 Answers2

1

The scope "view" on spring doesn´t exist... So you created your own, right? Just to check... I had that kind of problem once, and I think it was something to do with validation... the immediate=true attribute solved my problem.

Marco Noronha
  • 115
  • 1
  • 9
  • 31
0

This is something to do with the scope of the page. Your problem is due to partial rendering of page. Initially when you load the page it is not getting loaded fully and because of that the button is not part of that particular view when you try to click on the button for the first time. Try to make you view proper or explicitly render the page from the backing bean before displaying the page

arun_kk
  • 382
  • 2
  • 5
  • 14