概述
最近在做一个基于Primefaces 3.4.2+EJB的项目,项目中用到了Primefaces作为前端,个人觉得Primefaces可能还处于"完善阶段",小的bug非常的多,在做文件上传的相关模块时用了很多的时间去调试文件上传时"文件为空"的问题,下面对文件的上传与下载做一个完整的总结,具体步骤如下:
(1)JSF Bean
public class FileUploadController {
private UploadedFile uploadedFile;
public FileUploadController() {
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void submit() {
// Get information you from the uploaded file
System.out.println("Uploaded file name : " + uploadedFile.getFileName());
}
}
(2)JSF page:关于页面有两点需要注意1,form 必须包含"enctype"属性 2 h:commandButton 必须有ajax="false"属性
<h:form id="welcomeForm" enctype="multipart/form-data"
>
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" ajax="false" />
<h:message for="welcomeForm" />
</h:form>
(3)JSF 配置
基本配置如下:
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
以下的属性默认的情况下不需要配置
thresholdSize specifies the maximum file size in bytes to keep uploaded files in memory. If it
exceeds this limit, it’ll be temporarily written to disk.
uploadDirectory is the folder where to keep temporary files that exceed thresholdSize.
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:etc</param-value>
</init-param>
</filter>
(4)相关的JAR 添加 commons-fileupload-1.x.x.jar 以及commons-io-2.x.jar 到WEB-INF/lib/下
最后
以上就是魁梧季节为你收集整理的JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结的全部内容,希望文章能够帮你解决JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复