I have a JSF page that has a h:form that has some textfields and also an imageupload gadget from primefaces
My question is:
How can i get the text from the fields and assign them to some variable.(The objective is to separate the uploaded image/s from the text)
-What should i do?
-Do i need a filter, for that?
-Is there any easy way to achieve it with primefaces fileupload tool?
This is not trivial with PrimeFaces 2.2.1. The PF 2 file upload handling is a bit an epic fail. Under the covers it’s already not trivial to upload files with Ajax. The
XMLHttpRequestobject simply doesn’t supportmultipart/form-datarequests. Lot of “Ajaxified” (to have the feeling of an asynchronous progress) file upload solutions are based on a hidden iframe or -as in case of PF- using Flash. The PF 2 one is based on Flash and this is not combinable with regular text inputs in order to submit all the data in one go.In PF 3 the file upload component has been greatly revised. Using “simple” file upload mode your problem should be solved. However, PF 3 is currently still in beta/alpha stage. You would need to do a lot of unit tests on your webapp to see if it doesn’t break on PF 3.
If PF 3 is not an option, then your best bet is using Tomahawk 2.0 or homebrewing a JSF component.
As to the filter, under the covers the
FacesServletusesHttpServletRequest#getParameter()to retrieve the submitted values. When you’re familiar with basic JSP/Servlet, you should know how this usually works. The default HTML form encoding isapplication/x-www-form-urlencoded. ThegetParameter()method is relying on this. However, to be able to send binary data along this, such as file uploads, this default form encoding is unsuitable. For thismultipart/form-datashould be used instead.However, with this form encoding the
getParameter()calls won’t work anymore then. They will all returnnull. At its simplest form, you would need to parse the request body manually on a per-request basis. See also How to upload files to server using JSP/Servlet? However, this doesn’t work out in combination with JSF as it is relying ongetParameter()calls in order to set the bean properties (model values) with submitted values and to invoke the command button/link action.So you would like to change the
HttpServletRequestthat way so that thegetParameter()calls returns the proper values. For that aFilteris the right choice as it runs before any servlet such as theFacesServlet. The filter should then parse themultipart/form-datarequest body, create a parameter map and wrap and replace the originalHttpServletRequestwith a custom implementation which returns the right parameters and pass that request object instead through the chain so that JSF can use it fully transparently “the usual way”.