Advanced fileUpload get the file null with Primefaces [duplicate] - jsf

This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 5 years ago.
Environment:
Primefaces 6.1
JavaEE 7
Wildfly 10
JSF 2.2
when I click the button after having selected the file, the file var is null.
Test.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
<title>Application</title>
</h:head>
<h:body>
<h:form id="frmTest" enctype="multipart/form-data">
<ui:include src="template/messages.xhtml" />
<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="messages" sizeLimit="100000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
</h:form>
</h:body>
</f:view>
</html>
TestBean.java
#ViewScoped
#ManagedBean
public class TestBean implements Serializable {
...
public void handleFileUpload(FileUploadEvent event) {
UploadedFile file = event.getFile();//file is null
FacesUtils.addInfoMessage("Succesful" + file.getFileName() + " is uploaded.");
System.out.println("Succesful" + event.getFile().getFileName() + " is uploaded.");
}
...

You should remove or fill the update="" attribute from your fileUpload component. It "redirects" the files to a component after upload.

Related

using javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL causes viewParam to be null

I'm trying to switch an application from WildFly 13 (Java EE 7, JSF 2.2.15) to WildFly 16 (Java EE 8, JSF 2.3.9). Tried both PrimeFaces 6.2 and 7.0
In web.xml javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set.
JSF 2.2 version working fine, switching to WildFly 16 this different behavior. A simple example follows:
Instructions:
access: http://localhost:8080/primefaces-test/?tipo=U&test=Bah
click first button, click second button. after ajax the viewParam value in the bean is null, even it is filled within the url.
Sample project (Jetty): https://github.com/erickdeoliveiraleal/primefaces-test/tree/update
XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
<f:viewParam name="test" value="#{testView.testString}" />
<f:viewAction action="#{testView.inicializar}" />
</f:metadata>
<h:head>
<title>PrimeFaces Test</title>
</h:head>
<h:body>
<h:form id="cadastro">
<h:commandButton value="click me"
action="#{testView.inserirNaLista()}">
<f:ajax execute="#form" render="#form" />
</h:commandButton>
<p:commandButton
value="click me - 2 (this button causes the null value)"
action="#{testView.inserirNaLista()}" />
</h:form>
</h:body>
</html>
Bean
#ManagedBean(name = "testView")
#ViewScoped
public class TestView implements Serializable {
private String testString;
public void inicializar() {
System.out.println("initializing: " + testString);
}
public void inserirNaLista() {
System.out.println(testString);
}
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
}
fixed in WildFly 21 https://github.com/eclipse-ee4j/mojarra/issues/4550 and still pending official release in eclipse-ee4j

Getter method returns null when called from action listener [duplicate]

This question already has answers here:
How to choose the right bean scope?
(2 answers)
Closed 4 years ago.
I have an application with a p:selectOneMenu component. This component is used to determine what category of file is being uploaded so I can do some work to it when the file is uploaded.
I implemented the answer from this post and it seems to call my setter methods correctly for fileType. But once the file is submitted and the handleFileUpload method is called, the fileType getter method returns null.
For example, if I select Foo then I get the output
File type changed to: Foo
But when I hit the upload button I get the output
The file type selected is null
When I expect
The file type selected is Foo
What is causing the get method to return two different results and is there a way to fix this?
main.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<meta http-equiv="refresh"
content="#{session.maxInactiveInterval};url=index.xhtml" />
<f:view contentType="text/html">
<h:head>
<title>File Upload</title>
</h:head>
<p:layout fullPage="true">
<p:layoutUnit position="center">
<ui:insert name="pagebody" />
</p:layoutUnit>
</p:layout>
</f:view>
</html>
index.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" template="/templates/main.xhtml">
<ui:define name="pagebody">
<h:body>
<h:form id="uploadform" enctype="multipart/form-data">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="File Type:" />
<p:selectOneMenu value="#{uploadBean.fileType}">
<f:selectItem itemLabel="Foo" itemValue="Foo"/>
<f:selectItem itemLabel="Bar" itemValue="Bar"/>
<f:ajax listener="#{uploadBean.changeFileType}" />
</p:selectOneMenu>
</h:panelGrid>
<br />
<p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload}" mode="advanced"/>
</h:form>
</h:body>
</ui:define>
</ui:composition>
UploadBean.java
#RequestScoped
#ManagedBean(name = "uploadBean")
public class UploadBean implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String fileType = null;
#PostConstruct
public void init() {
}
public void handleFileUpload(FileUploadEvent event){
System.out.println("The file type selected is " + this.getFileType());
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public void changeFileType() {
System.out.println("File type changed to: " + this.getFileType());
}
}
As pointed out by #Kukeltje the issue was that my bean was not properly scoped. Switching from RequestScoped to ViewScoped fixed the issue I was having.

Primefaces file upload not working [duplicate]

This question already has an answer here:
Fileupload does not work in Primefaces
(1 answer)
Closed 5 years ago.
Trying to get jsf fileUploadListener to work just does not seem to do anything trying to just get a printline when hitting {fileUploadView.handleFileUpload} nothing seems to be happening.
UI loads up fine on localhost:8080/index.jsf after clicking on choose file I select a file then after pressing upload button nothing happens no errors in logs or print lines printing everything from display point of view seems to be fine.
index.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<link type="text/css" rel="stylesheet" href="/project/faces/javax.faces.resource/theme.css?ln=primefaces-glass-x" />
<f:view>
<h:head>
<meta charset="utf-8" />
<title>test</title>
</h:head>
<h:body>
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="true"
update="messages" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:growl id="messages" showDetail="true" />
</h:form>
</h:body>
</f:view>
</html>
FileUploadView.java (bean)
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
#ManagedBean
#ViewScoped
public class FileUploadView {
private UploadedFile uploadedFile;
public void handleFileUpload(FileUploadEvent event) {
System.out.println("handel file upoad =-=-=-=-=-");
FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
public UploadedFile getUploadedFile() {
System.out.println("uploading file get =-=-=-=");
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
System.out.println("uploading file set =-=-=-=");
this.uploadedFile = uploadedFile;
}
}
add enctype in your form.
enctype="multipart/form-data" in your <h:form>
replace you <h:form> with <h:form enctype="multipart/form-data">
their are two problems i see in your code
the first one is you should use
<h:form enctype="multipart/form-data">
your code here
</h:form>
and add ajax=false attribute to commandButton
<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>
official doc https://www.primefaces.org/showcase/ui/file/upload/basic.xhtml
Unrelated Ijust notice something funny, the upload doesn't work on primefaces official showcase xP

commandLink only works with a h and p version

I'm having this really weird issue where my commandLinkonly work if I have BOTH links in the same view.
Works fine, both links:
<ui:composition template="../../template.xhtml" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="content">
<h:form>
<p:dataTable var="v" value="#{myBean.myList}">
<p:column headerText="Name">
<h:commandLink value="Go to next page" action="#{myBean.doThing(v)}" />
<p:commandLink value="Go to next page" action="#{myBean.doThing(v)}" />
</p:column>
</p:dataTable>
<p:commandButton value="Submit" ajax="false" actionListener="#{myBean.submit}" />
</h:form>
</ui:define>
</ui:composition>
But if I remove either link, it doesn't even reach the method doThing() and reloads an empty page.
My bean:
#ManagedBean(name="myBean")
#RequestScoped
public class MyBean {
private List<Object> myList = new ArrayList<>();
public MyBean() {
}
public String doThing(Object obj) {
// This never prints if I remove one of the links
System.out.println("You're doing the thing!");
return "nextView?faces-redirect=true"
}
// Getters, setters etc.
}
I'm using JSF 2.0 and GlassFish 4.0.
How can I fix this and why is it happening?
I have a commandLink in a p:dataTable in another view and it's working fine.
EDIT:
Added the complete XML for the composition part of the view. The partial is inside a template:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<!-- css imports, titles etc -->
</h:head>
<h:body>
<ui:insert name="menu">
<ui:include src="./default/content.xhtml" />
</ui:insert>
</h:body>
</html>

JSF 2.2 multipart form is not getting submitted [duplicate]

This question already has an answer here:
Upgrade JSF / Mojarra in JBoss AS / EAP / WildFly
(1 answer)
Closed 7 years ago.
When I upload a file in jsf 2.2 project with jboss 7.1
its prevent to submit form.
help me out....
here is sample code for upload a file
This is my welcome.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
<h:form enctype="multipart/form-data" >
<h:outputLabel value="Upload File : " />
<h:inputFile value="#{myController.file}"/>
<h:commandButton value="Submit" action="#{myController.mycontroller}" />
</h:form>
</h:body>
</html>
Here is my backing bean MyController.java
public class MyController {
private Part file;
public Part getFile() {
return file;
}
public void setFile(Part file) {
this.file = file;
}
public String mycontroller() throws IOException {
System.out.println("--call mycontroller method--");
UploadFile uf = new UploadFile();
boolean b = uf.upload(getFile());
System.out.println(b);
return "success.xhtml";
}
}
Edit:1
I have one form with file upload and a <h:commandButton>, where I want to get uploaded file's information. Problem I am facing, the button is not firing when I added enctype="multipart/form-data". So any advice will be helpful to me.
I copy pasted your code and it worked as expected. The only addition I made is to add #ViewScoped and #ManagedBean annotation to MyController.

Resources