我已将<p:fileUpload />
组件插入到我的 JSF 2.1.5 应用程序中。我正在使用 PrimeFaces 3.4.1 。该组件由@ViewScoped
bean支持。我有这样的形式:
<?xml version='1.0' encoding='UTF-8' ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="/templates/general_template.xhtml">
<ui:define name="metadata">
<f:metadata>
<f:viewParam id="NavIndex" name="NavIndex"
value="#{navegableMassiveUserAdd._QueueIndex}" />
<f:event type="preRenderView"
listener="#{navegableMassiveUserAdd.initialize}" />
</f:metadata>
</ui:define>
<ui:define name="general_content">
<h:form>
<p:fileUpload
fileUploadListener="#{navegableMassiveUserAdd.listener}"
mode="advanced" allowTypes="/(\.|\/)(xls|csv)$/"
label="#{msg.SELECT}" multiple="false" />
<p:commandButton id="cancelButton"
action="#{navegableMassiveUserAdd.actionCancelAdd}"
value="#{msg.CANCELAR}" ajax="false" style="margin: 9px 0px 5px;" />
</h:form>
</ui:define>
这是我的主要模板:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui">
<h:head>
<meta http-equiv="Pragma" CONTENT="no-cache"></meta>
<meta http-equiv="cache-control" content="no-cache"></meta>
<meta http-equiv="Expires" CONTENT="-1"></meta>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-15" />
<h:outputStylesheet library="css" name="prime_styles.css" />
<h:outputScript library="js" name="prime_translations.js" />
</h:head>
<h:body>
<ui:insert name="metadata" />
<ui:insert name="general_logged_user">
<div align="right">
<span style="color: #B22222; padding: 3px;"> <h:outputText
value="#{loggedBean._Login}" />
</span> <span style="color: #FFA54F; padding: 3px;"> <h:outputText
value="Auth Method: #{loggedBean._Usuario._CUser._AuthenticationMethod}" />
</span>
</div>
</ui:insert>
<f:view locale="#{localeBean.locale}">
<ui:insert name="general_settings">
<f:loadBundle basename="resources.system.bundles.Messages" var="msg" />
<p:ajaxStatus style="width:0px;height:0px;" id="ajaxStatusPanel"
styleClass="spinner_ajax_loading">
<f:facet name="start">
<h:graphicImage value="/resources/images/loading.gif" />
</f:facet>
<f:facet name="complete">
<h:outputText value="" />
</f:facet>
</p:ajaxStatus>
<p:growl id="messages" autoUpdate="true" />
</ui:insert>
<f:event type="preRenderView"
listener="#{navigationManagerSystem.initialize}" />
<h:panelGroup>
<ui:insert name="general_header">
<ui:include src="/components/header.xhtml" />
</ui:insert>
<h:panelGroup id="menuNavegacionPanel">
<h:form>
<h:panelGroup id="navigationPanel"
rendered="#{navigationManagerSystem._ShowNavegacion}">
<h:panelGroup
rendered="#{!navigationManager._DisableNavigationButtons}">
<p:toolbar>
<p:toolbarGroup>
<ui:repeat value="#{navigationManagerSystem._Navegables}"
var="item">
<p:button value="#{item._Title}"
outcome="#{item._NavigationResult}">
<f:param name="NavIndex" value="#{item._QueueIndex}" />
</p:button>
</ui:repeat>
<p:button disabled="true"
value="#{navigationManagerSystem._Navegable._Title}" />
</p:toolbarGroup>
</p:toolbar>
</h:panelGroup>
</h:panelGroup>
</h:form>
</h:panelGroup>
<ui:insert name="general_content">
<ui:include src="/system/content.xhtml" />
</ui:insert>
</h:panelGroup>
<ui:insert name="general_footer">
<ui:include src="/components/general_footer.xhtml" />
</ui:insert>
</f:view>
</h:body>
有了这个,当我上传我的文件时,正在重建bean并且没有调用listener
方法。我将过滤器放在 web.xml 中。此外,如果我更改我的表单并将其设置为enctype="multipart/form-data"
,正如我在文档中所读到的那样,我无法避免此问题并且bean会再次重建,在这种情况下即使我单击取消按钮也是如此。这是我的bean代码,其中SystemNavegable
是一个抽象类:
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class NavegableMassiveUserAdd extends SystemNavegable {
/**
* Field logger.
*/
protected final Log logger = LogFactory.getLog(this.getClass());
/**
* Constructor for NavegableMassiveUserAdd.
*/
public NavegableMassiveUserAdd() {
super();
this.set_Title(FacesUtils.getBundle(BundleNames.BUNDLE_SYSTEM_MENU,
BundleKeys.SYSTEM_NAVEGABLE_ADD_USERS, "Añadir usuarios"));
this.set_TitleDescription(FacesUtils.getBundle(BundleNames.BUNDLE_SYSTEM_MENU,
BundleKeys.SYSTEM_NAVEGABLE_ADD_USERS_DESCRIPTION, "Añadir usuarios"));
this.set_NavigationResult(NavigationResults.MASSIVE_USER_ADD);
this._Behaviour = NavegableBehaviour.ADD;
}
/**
* Method actionCancelAdd.
*
* @return
*/
public String actionCancelAdd() {
return this._NavigationManagerSystem.cancelNavegable();
}
/**
* Escuchador del evento Upload.
*
*
*
* @param event
* FileUploadEvent
* @throws IOException
* @throws java.io.IOException
*/
public void listener(FileUploadEvent event) throws IOException {
UploadedFile uploadItem = event.getFile();
//More code
}
有什么建议吗?提前谢谢。
EDITED
完成了更多的测试,我发现永远不会达到文件上传监听器方法,我给予支持bean的范围并不重要。我有以下依赖项:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
我怀疑问题可能是另一个与我上传组件as is told here相冲突的过滤器。无论出现什么问题,我都会在上传文件时再次调用我的bean的初始化方法,并且FacesContext.getCurrentInstance().isPostback()
为false。
答案 0 :(得分:3)
经过几次头脑风暴,我发现这是一个过滤问题。由于上传过滤器与PrettyFaces过滤器冲突,我必须将其添加到我的 web.xml :
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
非常感谢@Reinaldo de Carvalho的回复
答案 1 :(得分:0)
在我的情况下,问题出在Tomcat服务器POST大小限制中。 因此,在设置了server.xml中的Connector部分的参数maxPostSize之后 文件上传开始与@ViewScoped bean一起使用。
另请注意,tomcat官方文档不正确,对于无限大小的POST,maxPostSize值必须设置为小于0,因为当它等于0表示零大小。
http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html HttpRequest maximum allowable size in tomcat?