p:commandButton with p:fileDownload and no ajax only works in second click - jsf

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>

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.

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

Related

Primefaces Command button causes page reloading

I am working an primefaces application, I am using
Primefaces 5.x version, Jsf 2.x
which has tabview
in one of the tabs I have command button, by clicking on it pdf export should happen.
For the first time click on button page is getting reloaded, from the second time on words request is going to bean page and downloading is working correctly.
this is my code snippet for command button :
<h:form id="mainForm">
<p:tabView id="myId" activeIndex="#{bean.currentTabIndex}" dynamic="true" cache="false">
<p:tab title="vehicle" id="vehId" styleClass="panelBack">
<h:form id="exportForm">
// some code
<p:commandButton value="export" process="#this" actionListener="# {bean.export()}" ajax="false" id="exportButton" />
</h:form>
</h:form>
If I make dynamic = false, it's working. I mean after clicking export button request is going to bean class, export is working.
What would be the problem? Can somebody help me?
Thank you
Yes your problem is most likely nested that is not allowed in JSF.

Primefaces form is not updating from within dialog

I have a view like so:
<h:form id="productForm">
<p:messages
id="productFormMessages"
autoUpdate="true"
redisplay="true"
globalOnly="true" />
<p:panelGrid
id="detailsGrid">
<p:row>
...
<p:column>
...
<p:outputLabel
id="fieldToUpdateId"
value="XYZ" />
<p:commandButton
action="#{bean.prepareDialog}"
update="dialogFormId"
oncomplete="PF('dialogModal').show();" />
</p:column>
</p:row>
</p:panelGrid>
</h:form>
<ui:include src="/../.../dialog.xhtml" />
And this is what dialog.xhtml looks like:
<p:dialog
id="dialogId"
widgetvar="dialogModal"
appendTo="#(body)"
modal="true"
dynamic="true">
<h:form
id="dialogFormId">
...a selectOneMenu...
<p:commandButton
id="saveButtonId"
value="Save"
actionListener="#{bean.save}"
oncomplete="PF('dialogModal').hide();"
process="#form"
update=":productForm" />
</h:form>
But the save button just refuses to update the productForm. It does update productFormMessages (which I'm guessing is because of the autoUpdate) but the form just doesn't update. I need to do a page reload from the browser in order for it to reflect the latest data. I have tried many different ways of doing this.
1. Using p:component to directly refer to the outputLabel in the update attribute.
2. (1) also using the form ID and the panelGrid ID.
3. Using the full client ID (with and without the preceeding ':') in the update attribute. Similarly using the form ID and the panelGrid ID.
4. Adding the client ID to the render IDs in the bean and then calling the partialViewContext#update method on that.
4. Setting partialViewContext#setRenderAll to true.
4. And as a last ditch attempt, as demonstrated by the code, updating the entire productForm. Which isn't so bad, because fieldToUpdateId is just one of the fields that I need to update in the form. But either way, it's not working.
Clearly, the data is being saved on the back end, because a page reload gives me the latest data. But I just can't get the dialog to update the form where it's being called from. I'm out of ideas. What am I doing wrong?
Theoretically this should work, But please inspect the button in the Html source, to make sure which part of the html it refresh. it should have something like this.
onclick="PrimeFaces.ab({s:'dialogFormId:saveButtonId1',u:'productForm',onco:function(xhr,status,args){PF('dialogModal').hide();;}});return false;"

Non-ajax method giving same result in ViewScoped managed bean

I have a dataTable like following
<p:dataTable value="#{userTable.tempLists}" var="user"
sortBy="#{user.userId}" sortOrder="descending"
paginator="true"
rows="10" paginatorPosition="bottom">
<p:column headerText="User ID" sortBy="#{user.userId}">
<p:commandLink ajax="false" value="#{user.userId}" action="#{userTable.showDetails(user.userId)}"/>
</p:column>
<p:column headerText="User Name" sortBy="#{user.userName}">
<h:outputText value="#{user.userName}">
</h:outputText>
</p:column>
</p:dataTable>
And I have a panel name as details like following
<p:panel>
<h:outputText value="#{userTable.userId}"/>
<h:outputText value="#{userTable.userName}"/>
</p:panel>
My UserTable managed bean is view scoped. Functionality is like when user will click on commandLink that it will my method will get details of user. After getting details dataTable will hide and panel will be shown. For user it almost seems like a different page.
Problem is here that when user will click any userId(commandLink) he will get details perfectly. Then he can use browser back button and try to get details by clicking another userId. But in this case user is getting same result as previous one. And nothing there in error log.
I found somewhere that in view scope managed bean this problem exist with non-ajax action method. But I need to keep it in viewscoped to maintain table state with sorting and pagination. So whenever user press back button I can show same screen before selection.
Please help. Thanks in advance.
The back button on a keyboard doens't really invoke a NEW HTTP request. Guessing this is what's going wrong. It's actually beter to use a list and details page. I've been struggling with the same error myself for some hours.
See: Difference between jsf actionListener(param) and propertyListener when using back button on keyboard

primefaces panelgrid update from datatable

I am getting "Cannot find component with identifier "contentForm:tabView:form:addressDialogPanel" referenced from "contentForm:tabView:form:addressBookTable" " error. How can I update my panelGrid inside the widget?
<h:form id="form">
<p:dataTable id="addressBookTable">
<p:ajax event="rowSelect" listener="#{addressBookController.onRowSelect}"
update="contentForm:tabView:form:addressDialogPanel" oncomplete="addressDialog.show()" />
</p:dataTable>
<p:dialog id="addressDialogId" widgetVar="addressDialog">
<h:panelGrid id="addressDialogPanel" columns="2" cellpadding="4">
</h:panelGrid>
</p:dialog>
</h:form>
The main problem is that you are giving the wrong client ID of component's. Also p:tabView is a component it is not a form. When you define a h:form, it generates a standart HTML form element. And when you submit the page JSF uses POST to submit your data into backing bean. So nesting them is going to occur lot's of issues that you don't expect. You should seperate forms into sections like sideForm or searchForm or etc.
You should detect the correct client ID of your component when you try to update it. You can do this with your browser's developer settings(press f12 for chrome). Then select component with the magnifier button and give that ID into update property. Just like here:
You should read to learn basics of JSF for example from here

View scoped bean is instantiated twice on one page, if there is no redirect when going to page

I have a page where I use #ViewScoped bean. On page I get data from the bean, and call action on it. Something like this:
<h:form>
<h:dataTable value="#{partyBean.foundOrganisations}" var="party">
<h:column headerClass="header">
<f:facet name="header">
#{msgs['company.registration.number']}
</f:facet>
<h:commandLink value="#{party.registrationNumber}" action="#{partyBean.select}">
<f:setPropertyActionListener value="#{party}" target="#{partyBean.selectedParty}" />
</h:commandLink>
</h:column>
</h:dataTable>
</h:form>
partyBean is #ViewScoped. When I get to this page by navigation rule with <redirect />, everything works fine, but when there is no <redirect />, foundOrganisations are taken from one instance of the bean, but setSelectedParty is called on another instance of the bean when link is clicked. Checked by debug.
Why is that? Can I do something to make it work without <redirect />?
I think its perfectly normal. partyBean is renew with new view.
For a submit to same page you can change your commandLink with command button and return null in action method. In this way JSF acts like same view.
Or you can try keep selectedparty value in a hiddenField (not tested in JSF 2.0)

Resources