开发者

How to extract text from forms marked with enctype:multipart/form-data(JSF 2.0)

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 f开发者_运维技巧rom 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 XMLHttpRequest object simply doesn't support multipart/form-data requests. 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.

  • JSF 2.0 File upload - Tomahawk 2.0 file upload tutorial
  • Uploading files with JSF 2.0 and Servlet 3.0 - Custom file upload component tutorial

As to the filter, under the covers the FacesServlet uses HttpServletRequest#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 is application/x-www-form-urlencoded. The getParameter() 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 this multipart/form-data should be used instead.

However, with this form encoding the getParameter() calls won't work anymore then. They will all return null. 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 on getParameter() 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 HttpServletRequest that way so that the getParameter() calls returns the proper values. For that a Filter is the right choice as it runs before any servlet such as the FacesServlet. The filter should then parse the multipart/form-data request body, create a parameter map and wrap and replace the original HttpServletRequest with 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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜