如何从标有enctype的表单中提取文本:multipart / form-data(JSF 2.0) [英] How to extract text from forms marked with enctype:multipart/form-data(JSF 2.0)

查看:228
本文介绍了如何从标有enctype的表单中提取文本:multipart / form-data(JSF 2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSF页面啊:表格有一些文本字段,还有一个来自 primefaces 的图像上传小工具

I have a JSF page that has a h:form that has some textfields and also an imageupload gadget from primefaces

我的问题是:
如何从字段中获取文本并将其分配给某个变量。(目的是分离上传的图像/ s来自文本)

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)

- 我该怎么办?

- 我需要一个过滤器吗?

-Do i need a filter, for that?

- 使用primefaces fileupload工具有没有简单的方法来实现它?

-Is there any easy way to achieve it with primefaces fileupload tool?

推荐答案

这对于PrimeFaces 2.2.1来说并非无足轻重。 PF 2文件上传处理有点像史诗般的失败。在幕后,使用Ajax上传文件已经不是一件容易的事了。 XMLHttpRequest 对象根本不支持 multipart / form-data 请求。许多Ajaxified(具有异步进展的感觉)文件上传解决方案基于隐藏的iframe或-as在PF-使用Flash的情况下。 PF 2一个基于Flash,并且不能与常规文本输入组合,以便一次性提交所有数据。

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.

在PF 3中文件上传组件已经大幅修改。使用简单文件上传模式,您的问题应该得到解决。然而,PF 3目前仍处于beta / alpha阶段。您需要在您的webapp上进行大量的单元测试,看看它是否在PF 3上没有中断。

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.

如果PF 3不是一个选项,那么你的最好的选择是使用Tomahawk 2.0或自制一个JSF组件。

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

关于过滤器, FacesServlet 使用 HttpServletRequest#getParameter() 检索提交的值。当您熟悉基本的 JSP / Servlet 时,您应该知道这通常是如何工作的。默认的HTML表单编码是 application / x-www-form-urlencoded getParameter()方法依赖于此。但是,为了能够沿此传输二进制数据,例如文件上传,此默认表单编码为不适合。为此,应该使用 multipart / form-data

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.

但是,使用此表单编码时, getParameter()调用将不再起作用。它们都将返回 null 。在最简单的形式中,您需要基于每个请求手动解析请求正文。另请参见如何使用JSP / Servlet将文件上传到服务器?然而,这不能与JSF结合使用,因为它依赖于 getParameter()调用以便设置提交的bean属性(模型值)值并调用命令按钮/链接操作。

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.

所以你想以这种方式更改 HttpServletRequest ,以便 getParameter() 调用返回正确的值。为此,过滤器是正确的选择,因为它在之前运行任何servlet,例如 FacesServlet 。然后过滤器应该解析 multipart / form-data 请求体,创建一个参数映射并换行并替换原来的 HttpServletRequest 使用自定义实现,返回正确的参数并通过链传递该请求对象,以便JSF可以通常的方式完全透明地使用它。

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".

这篇关于如何从标有enctype的表单中提取文本:multipart / form-data(JSF 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆