There are a couple of famous tutorial out there (such as BalusC’s) that show how to use Tomahawk’s component to upload files.
Some users use those tutorials to develop their own applications, but can’t get the inputFileUpload
to work. They got the configuration right, all the jars are at the right place, their web.xml
is perfectly tuned, they put enctype="multipart/form-data"
in the form tag, but still that damn component doesn’t work and the uploaded file is always null
, thus causing NullPointerException
when trying to save it.
I went there. And I found out some factors that cause the t:inputFileUpload
not to work.
Only one t:inputFileUpload
that save a certain file can belong to a form. In other words, one form cannot contain two distinct t:inputFileUpload
components that save the same file in the backing bean.
Here are some examples:
This will not work (the uploaded file will be null
), because the form contains two distinct t:inputFileUpload
mapped on the same file:
<body> <f:view> <h:form enctype="multipart/form-data"> <t:inputFileUpload value="#{myBackingBean.upFile}" /> <h:commandButton action="#{myBackingBean.upload}" value="Upload" /> <t:inputFileUpload value="#{myBackingBean.upFile}" /> <h:commandButton action="#myeBackingBean.upload}" value="Upload" /> <h:messages infoStyle="color: green;" errorStyle="color: red;" /> </h:form> </f:view> </body>
The following will work, because, even though the two t:inputFileUpload
are mapped on the same file, they belong to different forms.
<body> <f:view> <h:form enctype="multipart/form-data"> <t:inputFileUpload value="#{myBackingBean.upFile}" /> <h:commandButton action="#{myBackingBean.upload}" value="Upload" /> <h:messages infoStyle="color: green;" errorStyle="color: red;" /> </h:form> <h:form enctype="multipart/form-data"> <t:inputFileUpload value="#{myBackingBean.upFile}" /> <h:commandButton action="#myeBackingBean.upload}" value="Upload" /> <h:messages infoStyle="color: green;" errorStyle="color: red;" /> </h:form> </f:view> </body>
The following will work, because, despite the two t:inputFileUpload
appear in the same form, they are mapped on different files.
<body> <f:view> <h:form enctype="multipart/form-data"> <t:inputFileUpload value="#{myBackingBean.upFile1}" /> <h:commandButton action="#{myBackingBean.upload1}" value="Upload" /> <t:inputFileUpload value="#{myBackingBean.upFile2}" /> <h:commandButton action="#myeBackingBean.upload2}" value="Upload" /> <h:messages infoStyle="color: green;" errorStyle="color: red;" /> </h:form> </f:view> </body>