所以,这个问题非常普遍。我想使用p:fileUpload
(showcase)中的mode="simple"
将文件上传到服务器。关于在论坛上上传文件的问题有很多问题和答案,但我找不到全面的问题。文件上传组件位于/faces/second.xhtml
:
<h:form>
<p:wizard>
<p:tab id="firstStep" title="First step">
<ui:include src="/faces/first.xhtml" />
</p:tab>
<p:tab name="secondStep" title="Second step">
<ui:include src="/faces/second.xhtml" />
</p:tab>
</p:wizard>
</h:form>
/faces/second.xhtml
包含的位置:
<p:panel>
<h:panelGrid columns="2" id="file">
<p:fileUpload value="#{tutorialBean.thumbnail}" mode="simple" />
<p:commandButton value="Submit" update="growl"
actionListener="#{tutorialBean.thumbnailUpload}" />
</h:panelGrid>
</p:panel>
Bean:
@SessionScoped
public class TutorialBean {
private UploadedFile thumbnail;
public void thumbnailUpload(){
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
"File uploaded", thumbnail.getFileName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
// setters and getters
}
我已将FileUpload过滤器添加到web.xml
:
<!-- PrimeFaces file upload -->
<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>100000</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:\Users\name\Desktop\</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
我已将commons-io
(2.0.1)和commons-fileupload
(1.2.2)添加到我的pom.xml
。根据解决方案,我发现它必须有效,但事实并非如此。在thumbnail
执行期间,null
为thumbnailUpload
。
我使用的是primefaces 3.0.M4,jBoss为7.0.2.Final
提前致谢
已解决:问题不在fileUpload
组件中,而在于页面中的形式,该页面包含在主页面中。我已将prependId="false"
添加到内部表单中,一切正常
答案 0 :(得分:1)
首先尝试使用简单/标准Primefaces过滤器:
<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>
我真的不知道为什么它不适合你。我尝试了简单和高级模式,两者都很好。此外,如果您使用高级模式,您可以使用接收FileUploadEvent event
作为参数的操作方法,这样更容易:
String fileName = "Uploaded_"+event.getFile().getFileName();
System.out.println("Uploaded: " + fileName);
更新:
因为你在第二步中只有文件上载,你可以尝试这样的事情。
<p:wizard>
<p:tab id="firstStep" title="First step">
<h:form>
<ui:include src="/faces/first.xhtml" />
</h:form>
</p:tab>
<p:tab name="secondStep" title="Second step">
<p:panel>
<h:form multipart>
<h:panelGrid columns="2" id="file">
<p:fileUpload value="#{tutorialBean.thumbnail}" mode="simple" />
<p:commandButton value="Submit" update="growl"
actionListener="#{tutorialBean.thumbnailUpload}" />
</h:panelGrid>
</h:form>
</p:panel>
</p:tab>
</p:wizard>