我正在使用Primefaces 4.0在Apache Tomcat 7上运行web应用程序。 试图实现类似于Primefaces ShowCase的上传界面,上传界面不会激活支持bean中的任何功能:
这是我直接包含在我体内的内容(我也试过没有“多个”,“值”,高级模式,“actionListener”属性)
<p:fileUpload id="fileupload_"
value="#{userWizard.fu.uploadedFile}"
fileUploadListener="#{userWizard.fu.addFileToAttachment}"
actionListener="#{userWizard.fu.listener}"
mode="advanced"
dragDropSupport="false"
update="messages"
multiple="false"
sizeLimit="50000000"
fileLimit="3"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
auto="true"/>
<p:growl id="messages" showDetail="true" />
这是我的支持豆:
import java.util.LinkedList;
import java.util.List;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
@ManagedBean
@SessionScoped
public class FileUpload {
private UploadedFile uploadedFile;
List<UploadedFile> files = new LinkedList<UploadedFile>();
public FileUpload()
{}
public List<UploadedFile> getFiles() {
System.out.println( "< getFiles > ");
return files;
}
public void setFiles(List<UploadedFile> files) {
System.out.println( "< setFiles > ");
this.files = files;
}
public void addFileToAttachment(FileUploadEvent event)
{
System.out.println("addfile");
System.out.println( "upload > " + event.getFile().getFileName());
files.add(event.getFile());
}
public UploadedFile getUploadedFile() {
System.out.println( "< getFile > ");
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
System.out.println( "< setFile > ");
this.uploadedFile = uploadedFile;
}
public void listener(ActionEvent ae)
{
System.out.println("listener");
}
public void insert()
{
System.out.println( "insert");
if(uploadedFile !=null)
{
System.out.println( " > " + uploadedFile.getFileName());
}
}
}
我在网页上看到了上传界面,文件似乎已上传,但我的输出中没有打印出任何侦听器/设置器println。在我的FileUpload bean中使用其侦听器的常规命令按钮会正常触发它。
使用Primefaces 4.0,我没有在我的web.xml文件中添加FileUpload过滤器(我也尝试添加它以防万一,但它没有更好的工作)。
另外,commons-fileupload-1.3.1.jar和commons-io-2.4.jar都在我项目的库中。
有没有人知道我的问题可能在哪里?
谢谢
答案 0 :(得分:0)
经过多次检查,我意识到我使用的是javax.faces-2.1而不是2.2。 结合javax.servlet API并仍在Primefaces 4.0中,我的问题现在似乎已经解决了。
答案 1 :(得分:0)
在您的代码fileUploadListener="#{userWizard.fu.addFileToAttachment}"
中,您只需发布FileUpload.java
即可。
我没有看到你的userWizard
bean和addFileToAttachment
监听器方法。但是,请尝试以下
配置web.xml
配置如下:
<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>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
您的h:form
必须与<h:form enctype="multipart/form-data">
相似。
对于多个上传示例:
multipleupload.xthml
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{MultipleUploadActionBean.handleProposalAttachment}"
mode="advanced" multiple="true" sizeLimit="3000000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" id="attachment"/>
</h:form>
MultipleUploadActionBean.java
@ManagedBean(name = "MultipleUploadActionBean")
@ViewScoped
public class MultipleUploadActionBean {
private List<UploadedFile> uploadFileList = new ArrayList<UploadedFile>();
public void handleProposalAttachment(FileUploadEvent event) {
UploadedFile uploadedFile = event.getFile();
uploadFileList.add(uploadedFile);
}
}
对于单个上传示例:
singleupload.xthml
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{SingleUploadActionBean.uploadedFile}" mode="simple"/>
<p:commandButton value="Submit" ajax="false" actionListener="#{SingleUploadActionBean.upload}"/>
</h:form>
SingleUploadActionBean.java
@ManagedBean(name = "SingleUploadActionBean")
@ViewScoped
public class SingleUploadActionBean {
private UploadedFile uploadedFile;
//getter & setter
public void upload() {
//your operation
}
}