PrimeFaces文件上传 [英] PrimeFaces fileupload

查看:159
本文介绍了PrimeFaces文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsp/Servlet到项目中,并且我决定使用PrimeFaces迁移到JSF,在尝试使用PrimeFaces fileupload控件上传文件时遇到了问题,然后将其配置为web.xml时效果很好,现在所有旧的jsp文件上传方式均无法正常工作的问题:

I am using jsp/Servlet into a project and I have decided to migrate to JSF using PrimeFaces ,I have faced a problem while trying to upload file using PrimeFaces fileupload control then when I have configured it into web.xml it worked well , the problem now that all old jsp file upload way is not working:

 DiskFileUpload upload = new DiskFileUpload();
 List items = upload.parseRequest(request);

推荐答案

实际上,一个HTTP请求只能被解析一次.如果您需要解析两次,客户端将不会再次发送它.您的问题表明,由于某种原因,PrimeFaces文件上载过滤器也由于对纯JSP/Servlet请求而被调用,因此在纯JSP/Servlet有机会对其进行解析以供自己使用之前,对JSF的上载进行了解析.这不应该发生.

Indeed, a HTTP request can be parsed only once. The client ain't going to send it for a second time if you need to parse it twice. Your problem indicates that the PrimeFaces file upload filter is for some reason also invoked on a plain JSP/Servlet request and thus parses the upload for JSF before plain JSP/Servlet get chance to parse it for own use. This should not happen.

您只需要将PrimeFaces文件上传过滤器映射到JSF请求,而不是简单的JSP/Servlet请求.通常,您可以将其映射到FacesServlet.

You need to map the PrimeFaces file upload filter on JSF requests only, not on plain JSP/Servlet requests. You normally achieve that by mapping it to the FacesServlet.

<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

(<servlet-name>必须与FacesServlet类的<servlet>定义中使用的值完全相同; <filter-name>显然与<filter>中使用的值完全相同>文件上传过滤器类的定义)

(the <servlet-name> must be exactly the same value as is been used in <servlet> definition of the FacesServlet class; the <filter-name> is obviously exactly the same value as is been used in <filter> definiton of the file upload filter class)

或者,如果出于某种显而易见的原因直接在JSF页面中使用了旧的JSP/Servlet文件上传方法,那么您需要将PrimeFaces文件上传过滤器映射到更具体的URL模式上,该模式仅覆盖包含PrimeFaces的页面文件上传组件.

Or, if that old JSP/Servlet file upload approach is been used straight in a JSF page for some unobvious reason, then you need to map the PrimeFaces file upload filter on a more specific URL pattern covering only the pages containing the PrimeFaces file upload component.

<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <url-pattern>/upload.xhtml</url-pattern>
</filter-mapping>

(如果将FacesServlet映射到例如*.jsf而不是*.xhtml,则显然应该将URL模式更改为/upload.jsf)

(if you've mapped the FacesServlet on for example *.jsf instead of *.xhtml, then you should obviously change the URL pattern to /upload.jsf)

请注意,您可以在单个过滤器映射上指定多个<url-pattern>条目,这在您有多个包含PrimeFaces文件上传组件的页面的情况下很有用.

Note that you can specify multiple <url-pattern> entries on a single filter mapping, which is useful for the case that you've multiple pages containing the PrimeFaces file upload component.

<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <url-pattern>/upload1.xhtml</url-pattern>
    <url-pattern>/upload2.xhtml</url-pattern>
    <url-pattern>/upload3.xhtml</url-pattern>
</filter-mapping>

这篇关于PrimeFaces文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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