Detect when the multiupload primefaces componet ends all the uploads - jsf-2

I´m usig the jsf version 2.1 primefaces 5.1 and tomcat 7.
I have to execute a managed bean method after the all the uploads has been succesfully in a multiple upload component, I used the onComplete atribute, but is executed after each upload. I need to do it after the all files upload was completed.
How identify that?
Thanks in advance for your time and answers
PD I posted this question in the primefaces 1 forum but nobody answer.

This should work:
<p:fileUpload ... oncomplete="doSomething(this);" />
<p:remoteCommand name="rc" actionListener="#{bean.method}" />
and
<script>
function doSomething(fileupload) {
if (fileupload.files.length == 0) {
rc();
}
}
</script>
On the bean:
public void method() {
// Do something here
}

Related

Render or update a page while still running inside a method

I was wondering if you could render or update a page while you still inside a method;
here is an example:
the .xhtml file:
<h:form id="form">
<h:commandButton value="change" actionListener="#{bean.changeText}">
<f:ajax render="out" />
</h:commandButton>
<h:outputLabel id="out" value="#{bean.text}" />
</h:form>
and here is the changeText Method:
public void changeText() throws InterruptedException{
text = "test1";
FacesContext.getCurrentInstance().renderResponse();
Thread.sleep(2000);
text = "test2";
}
Now is it possible to set the output label (id = out) to be rendered (or changed) to "test1", then after the 2 second have elapsed it changes to "test2"?
Well looking at the JSF life cycle i thought it isnt possible, but maybe i got the life cycle wrong, or one of you guys know a work around.
No, that's not possible. The render response only starts once the action method returns.
There are basically 2 ways to achieve the desired behavior: polling or pushing. Based on your question history, you're using Java EE 7 + JSF 2.2 + PrimeFaces. You could for pushing use PrimeFaces <p:socket> or homegrow one using Java EE 7's new WebSocket API (JSR-356).

Get file url in JSF 2.2

I am trying to make an openfiledialog in the backing bean, so that I can get the url of the local file. However it seems that the standard JSF library does not support open/save file dialog. Is there another way of getting an url of a file with something similar to openfiledialog?
Here is what has to happen:
Click on a button --> send action to the backing bean --> show openfiledialog --> get url of the file --> add url of the file to a list of strings.
I am not sure how to accomplish the bold steps above. Is there someone who can give me any leads or help to accomplish this?
(I am using JSF & Primefaces, I would prefer not to use another external library if possible.)
Since you're using JSF 2.2 as well as Primefaces, why not use the primefaces component itself?
Below is the Primefaces 5.0 showcase example for a basic file upload:
<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>
<p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" disabled="true" />
</h:form>
Managed Bean
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
#ManagedBean
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
The enctype attribute for the form is required. Also, for using Primefaces file upload, you need to setup certain context parameters and filters. You may refer the appropriate documentation for the same.
You can get url for file uploaded to path or database, not sure if you make a search in google but I found these URLs maybe will help you:
http://balusc.blogspot.com/2007/07/fileservlet.html
Download a file with JSF?
You just need to open a dialog have link of file, or maybe file url can be stored in variable to add to list too or what ever you want.
Hope this will help you.

Textbox Focus in Richfaces

I have make sample project of focus in textbox from the demo of Richfaces Showcase. I use JSF 2.0, Richfaces 4.3.0 Final.jar and Jboss 7 server. I can sucessfully run the project but focus did not work. When I press the tab key, focus arrives at the textbox. If there is anything I need to congifure, please advise me.
Thanks in advance.
Finally, I got it. I put the javascript code in the jsf form just like this.
<h:form id="frm">
<script type="text/javascript">
window.onload = function() {
document.getElementById('frm:txtStaffId').focus();
}
</script>
...
...
<h:inputText id="txtStaffId"/>
...
...
</h:form>

Grouping radio button inside data grid using primefaces 3.3 and jsf 2.0

I am new to jsf 2.0 and therefore i am facing a serious issue grouping the prime faces datagrid and radio buttons. I am using JSF 2.0-Primefaces 3.3, java 1.6, and running my application on tomcat server on windows operating system.
The issue is grouping data grid and radio button. The following paragraph will describe about an example code:
#ManagedBean(name="quickBill")
#ViewScoped
public class QuickBill extends BaseManagedBean implements Serializable {
private List<String[]> insurerIDs=null;
#PostConstruct
public void init(){
loadInsurer();
}
private void loadInsurer(){
logger.info("Entered Load Insurer");
insurerIDs = new ArrayList<String[]>();
if (null != insurerList) {
insurerIDs.add(new String[] {"InsurerID1","Insurer_Name1") });
insurerIDs.add(new String[] {"InsurerID2","Insurer_Name2") });
insurerIDs.add(new String[] {"InsurerID3","Insurer_Name3") });
insurerIDs.add(new String[] {"InsurerID4","Insurer_Name4") });
}
}
public List<String[]> getinsurerIDs() {
return insurerIDs;
}
}
The jsf code is as follows:
<p:dataGrid var="quickBillVar" value="#{quickBill.insurerIDs}" columns="4" >
<p:column selectionMode="single">
<p:selectOneRadio id="options" value="#{quickBill.selectedOption}">
<f:selectItem itemLabel="#{quickBillVar[1]}" itemValue="#{quickBillVar[1]}"/>
</p:selectOneRadio>
<p:commandLink id="insurerid" title="Isurer">
<p:graphicImage value="../../images/insurers1/#{quickBillVar[0]}.jpg"/>
</p:commandLink>
</p:column>
</p:dataGrid>
The above code when executed results in the display of the radio button, which allows user to select multiple radio button at the same time. However the requirement is to restrict user to select only one button rather than allowing user to select multiple button at a time. I have also used selectionMode="single" to produce the expected output but it didn't help much.
Kindly help me on this or provide me with examples which will overcome this issue.
I believe the custom layout in this example will help.
https://www.primefaces.org/showcase/ui/input/oneRadio.xhtml
You can try the below code using Map.
Backing bean code
String selectedInsurer;
Map<String,String> insurerIDs = new LinkedHashMap<String,String>();
insurerIDs.put("InsurerID1", "Insurer_Name1"); //(label, value)
insurerIDs.put("InsurerID2", "Insurer_Name2");
insurerIDs.put("InsurerID3", "Insurer_Name3");
insurerIDs.put("InsurerID4", "Insurer_Name4");
//-- getter & setter
At front end
<h:selectOneRadio value="#{quickBill.selectedInsurer}">
<f:selectItems value="#{quickBill.insurerIDs}" />
</h:selectOneRadio>

JSF p:remoteCommand and javascript event onbeforeunload

We have several web applications a user can login. On click on logout button some logic must be processed. The problem is that most users close the browser without clicking on logout button. I tried the following to call my logic on browser close:
Added the event "onbeforeunload" to my frameset. On browser close a logout function will be called.
Within my logout function I use the primefaces p:remoteCommand component to call a action listener on the server.
In current firefox version everything works fine but I have some problems with IE9. Closing a tab in IE9 calls my logic. Closing the browser doesn´t work. My JS function is called but the request to the server is not executed. Is there any way to solve this problem? BTW: I know that this is not an 100% solution but we need exactly this functionality. My function and p:remoteCommand looks like that.
function automaticLogout() {
handleAutomaticLogout();
alert('BlaBla');
}
<p:remoteCommand name="handleAutomaticLogout" actionListener="#{myBean.handleAutomaticLogout}" async="false" />
Do you specifically require a client-side Javascript solution or is this just the way you have gone about it so far?
On the Server Side, placing a #PreDestroy annotation above a Backing Bean method causes that method to be invoked just before the Bean goes out of scope.
If you write a method which invalidates the session (session.invalidate()) with this annotation, it will be called when the user leaves your page without clicking Log Out.
Backing Bean:
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
#ManagedBean
#SessionScoped
public class PreDestroyBean {
//Called by button - log out perhaps?
public void killTheSessionDeliberately() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();
}
#PreDestroy
public void revokeLicense() {
System.out.println("PreDestroy method was called.");
}
}
Page Button:
<h:form>
<p:commandButton id="killSession" value="Kill Session"
actionListener="#{preDestroyBean.killTheSessionDeliberately()}" update="#form" />
</h:form>
You can use p:remoteCommand along with onunload.
example:
<body onunload="test();">
<form id="exit">
<p:remoteCommand id="test" name="test" action="#{ContatoMB.teste}"/>
</form>
</body>
referencing http://forum.primefaces.org/viewtopic.php?f=3&t=15695
It works for me.

Resources