在JSF primefaces中上传文件 [英] Upload file in JSF primefaces
问题描述
我试图用一个简单的表单上传一个文件,但是它只是一直返回到同一个页面。为了找到错误或者其他的错误,我只想在输出中写上我上传文件的名字。一旦我能得到文件名,我想我可以处理如何处理整个文件。所以这是我的代码:
< h:form enctype =multipart / form-data>
< p:fileUpload value =#{jokoBerriaController.file}mode =simple/>
< / h:表格>
控制器:
import javax.faces.bean.ManagedBean;
import org.primefaces.model.UploadedFile;
@ManagedBean
public class jokoBerriaController {
public static UploadedFile file;
public UploadedFile getFile(){
return file;
}
public void setFile(UploadedFile file){
this.file = file;
$ b $ public void upload(){
System.out.println(file+ file.getFileName());
$ b $ p
$ b < 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>
在我的web.xml文件中,但是我没有添加commons-io和commons-文件上传库。我读过,我必须把我的pom.xml文件中的下一个代码,但我找不到该文件。
<依赖关系>
< groupId> commons-fileupload< / groupId>
< artifactId> commons-fileupload< / artifactId>
< version> 1.2.2< / version>
< /依赖>
<依赖>
< groupId> commons-io< / groupId>
< artifactId> commons-io< / artifactId>
< version> 1.4< / version>
< /依赖>
感谢您的帮助。
PD :对不起我的英文。
解决方案
你需要注意所有的细节。所有依赖关系必须包含在WEB-INF / lib中,并且必须正确声明web.xml中的servlet过滤器。
我遇到过类似的问题,按照该帖子的提示。
I'm trying to upload a file with a simple form but it just returns to the same page all the time. To find the mistake or whatever is wrong, I just want to write in the output the name of the file I upload. Once I can get the file name I guess I can handle how to handle the entire file. So this is my code:
<h:form enctype="multipart/form-data">
<h:outputText value="Argazkia: "/>
<p:fileUpload value="#{jokoBerriaController.file}" mode="simple"/>
<p:commandButton value="Bidali" ajax="false" actionListener="#{jokoBerriaController.upload()}"/>
</h:form>
The controller:
import javax.faces.bean.ManagedBean;
import org.primefaces.model.UploadedFile;
@ManagedBean
public class jokoBerriaController {
public static UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
System.out.println("file " + file.getFileName());
}
}
I had added
<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>
at my web.xml file, but i've haven't added commons-io and commons-fileupload libraries. I've read that i have to put the next code in my pom.xml file, but I can't find that file.
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
Thanks for your help.
PD: Sorry about my English.
解决方案
You need to pay attention to all the details. All dependencies must be included in WEB-INF/lib, and the servlet filter in web.xml must be declared correctly.
I had similar issues and I got it working by following the tips on that post.
这篇关于在JSF primefaces中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!