JSF - Primefaces FileUpload不触发方法 [英] JSF - Primefaces FileUpload does not trigger method

查看:173
本文介绍了JSF - Primefaces FileUpload不触发方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并尝试了每一个在网上的例子,我发现,但我的例子仍然无法正常工作。
我的web.xml



...(标题)

 <的context-param> 
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>开发< /参数值>
< / context-param>
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> *。xhtml< / url-pattern>
< / servlet-mapping>
< 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>
< listener>
< listener-class> registration.cleanRegisterDB< / listener-class>
< / listener>
< listener>
< listener-class> main.poolChecker< / listener-class>
< / listener>
< session-config>
< session-timeout> 30< / session-timeout>
< / session-config>
< welcome-file-list>
< welcome-file> faces / index.xhtml< / welcome-file>
< / welcome-file-list>
< listener>
< listener-class> Utils.SessionAtributeListener.MyAtributeListener< / listener-class>
< / listener>
< context-param>
< param-name> primefaces.PRIVATE_CAPTCHA_KEY< / param-name>
< param-value> ...< /参数值>
< / context-param>
< context-param>
< param-name> primefaces.PUBLIC_CAPTCHA_KEY< / param-name>
< param-value> ...< /参数值>
< / context-param>
<错误页面>
<错误代码> 404< /错误代码>
< location> /faces/404.xhtml< / location>
< / error-page>



我的xhtml页面: p>

 <?xml version ='1.0'encoding ='UTF-8'?> 
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
lang =#{main_bean.authLang()}
xmlns =http://www.w3.org/1999/xhtml
xmlns: h =http://java.sun.com/jsf/html
xmlns:ui =http://java.sun.com/jsf/facelets
xmlns:p =http ://primefaces.org/ui
xmlns:f =http://java.sun.com/jsf/core
xmlns:c =http://java.sun.com / JSP / JSTL /芯>

< f:view locale =#{main_bean.locale}encoding =ISO-8859-1>
< h:head>
< h:outputStylesheet library =theme1name =css / style.css/>
< h:outputScript library =theme1name =js / jquery.validate.js/>
< / h:头>

< h:body>
< f:event type =preRenderComponentlistener =#{postbean.loadMasterCategories()}>< / f:event>
< h:panelGroup id =layout-for-imagelayout =block>
< h:graphicImage name =theme1 / images / logo.png/>
< / h:link>
< / h:panelGroup>

< h:panelGroup class =layout-titleslayout =block>
< h2>#{msg.postInt}< / h2>
< / h:panelGroup>
< h:form id =formPostpoolenctype =multipart / form-data>
< p:growl id =growlaaautoUpdate =true/>
< p:confirmDialog id =confirmDialogmessage =#{msg.dialogConfPostInt}
header =#{msg.confirmDialogHeader}severity =alertwidgetVar =confirmation>
< h:panelGroup id =dialog-captcha-layoutlayout =block>
< / h:panelGroup>
< / h:panelGroup>

action =index ajax =falseonclick =confirmation.hide(); style =background-image:url('../ resources / theme1 / images / icons / yes.png')>
< / p:commandButton>

ajax =falsetype =buttononclick =确认.hide();/>
< / p:confirmDialog>

< h2>#{msg.infoProd}< / h2>
< h:panelGroup class =details-elementlayout =block>
< h:outputLabel value =#{msg.categoryPost}for =cat/>
< h:panelGroup class =element-inputslayout =block>
< p:selectOneListbox style =height:150px; id =catvalue =#{postbean.masterCategoria}converter =omnifaces.SelectItemsConverterrequired =truerequiredMessage =#{msg.message_radiobutton_choose}>
< / p:selectOneListbox>

< / h:panelGroup>
< / h:panelGroup>

< h:panelGroup class =details-elementlayout =block>
< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.titlePost}for =titulo/>
< / h:panelGroup>
< h:panelGroup layout =block>
< p:inputText id =titulovalue =#{postbean.titulo}styleClass =inputs-loginrequired =truerequiredMessage =#{msg.message_titulo_null}/>
< / h:panelGroup>
< / h:panelGroup>

< h:panelGroup class =details-elementlayout =block>
< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.descricaoPost}for =desc/>
< / h:panelGroup>
< h:panelGroup layout =block>
< / h:panelGroup>
< / h:panelGroup>

< h:panelGroup class =details-elementlayout =block>
< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.marcaPost}for =marca/>
< / h:panelGroup>
< h:panelGroup layout =block>
< p:inputText id =marcavalue =#{postbean.marca}styleClass =inputs-login/>
< / h:panelGroup>
< / h:panelGroup>

< h:panelGroup class =details-elementlayout =block>
< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.modeloPost}for =Modelo/>
< / h:panelGroup>
< h:panelGroup layout =block>
< p:inputText id =Modelovalue =#{postbean.modelo}styleClass =inputs-login/>
< / h:panelGroup>
< / h:panelGroup>

< h:panelGroup class =details-elementlayout =block>
< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.quantidadePost}for =qtdDesejada/>
< / h:panelGroup>
< h:panelGroup layout =block>
< f:validateDoubleRange minimum =0.00000001/>
<! - < f:validateRegex pattern =([1-9] [0-9] * \。?[0-9] *)for =qtdDesejada/> - - >
< / p:inputText>
< / h:panelGroup>
< / h:panelGroup>

< h:panelGroup class =details-elementlayout =block>
< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.precoTargetPost}for =precoTarget/>
< / h:panelGroup>
< h:panelGroup layout =block>
< f:validateDoubleRange minimum =0.00000001/>
< / p:inputText>
< / h:panelGroup>
< / h:panelGroup>
< h:panelGroup class =details-elementlayout =block>

< h:panelGroup class =element-labelslayout =block>
< h:outputLabel value =#{msg.fotosPost}for =foto/>
< / h:panelGroup>
<! - < h:panelGroup layout =blockstyle =display:inline-block; * display:inline; zoom:1; ID = addFotoPanel >
< h:inputFile id =idinputFilevalue =#{postbean.file}/>
< / h:panelGroup> - >
< p:fileUpload value =#{postbean.upfile}auto =truefileUploadListener =#{postbean.handleFileUpload}widgetVar =asd/>

< / h:panelGroup>

< p:commandButton id =btnvalue =#{msg.label_buttonsubmit}styleClass =h-button-searchonclick =confirmation.show();/>
< / h:panelGroup>
< / h:panelGroup>

< script type =text / javascript>
$(document).ready(function(){
$('#formPostpool')。keypress(function(e){
if(e.keyCode == 13){
event.preventDefault();
$(#btn)。click();
}
})
});
< / script>
< / h:表格>
< / h:body>
< / f:view>





和bean:
//...some import

$ p $ @ManagedBean(name =postbean)
@ViewScoped
public class postbean implements Serializable {

//////////////////////////////////
// FACADES
/////////////////////////////////
@EJB
ProdutoFacade produtofacade = new ProdutoFacade();
@EJB
UsersFacade userfacade = new UsersFacade();
@EJB
ImageFacade imagefacade = new ImageFacade();
@EJB
categoriasFacade categoriafacade = new categoriasFacade();
@EJB
buyerPoolFacade buyerPoolFacade = new buyerPoolFacade();
@EJB
LanguageFacade languageFacade = new LanguageFacade();
@EJB
Lingua_ProdutoFacade langProFacade = new Lingua_ProdutoFacade();
@EJB
MessagesFacade messagesFacade = new MessagesFacade();
/////////////////////////////////////
// VARIAVEIS
////////////////////////////////////
// Destino ondevãser ser guardadas as图像相对于池
私人字符串目的地=C:;
私人清单< String>网址;
私人字符串titulo;
私人字符串descricao;
私人字符串marca;
private String modelo;
私人浮动预科;
私人短期旅行
private int quantidadePretendida;
私人列表< categorias> categoria; // lista das categorias
私人时间戳dataActual;
私人用户publicationUser;
私人分类masterCategoria;
私人分类选项2;
私人列表< categorias>文本;
私人列表< categorias> subcat2;
private boolean rend;
private boolean rend2;
私人部分文件;
私人UploadedFile upfile;

public Part getFile(){
return file;
}

public void setFile(Part file){
this.file = file;

$ b $ public UploadedFile getUpfile(){
return upfile;
}

public void setUPfile(UploadedFile upfile){
this.upfile = upfile;

$ b $ public postbean(){...}

//...getters and setters

// .. .some方法
$ b $ public void handleFileUpload(FileUploadEvent event){

ExternalContext externalContext = FacesContext.getCurrentInstance()。getExternalContext();
HttpServletResponse response =(HttpServletResponse)externalContext.getResponse();
FacesContext ctx = FacesContext.getCurrentInstance();
Locale l = ctx.getViewRoot()。getLocale();
ResourceBundle rb = ResourceBundle.getBundle(languages.welcome,l);

FacesContext aFacesContext = FacesContext.getCurrentInstance();
ServletContext context =(ServletContext)aFacesContext.getExternalContext()。getContext();
copyFile(event.getFile()。getFileName(),event.getFile()。getInputstream());
FacesMessage msg = new FacesMessage(rb.getString(Success),event.getFile()。getFileName()+ rb.getString(wasUploaded));
FacesContext.getCurrentInstance()。addMessage(null,msg);
} catch(IOException e){
e.printStackTrace();



public void copyFile(String fileName,InputStream in){
String username = userfacade.findUserByIDSessao(FacesUtils.getSessionID());
OutputStream out = null;
try {
//将inputStream写入FileOutputStream
文件dir = new File(destination + username);
if(!dir.exists()){
dir.mkdir();
}
文件f =新文件(dir +\\+ fileName);
out = new FileOutputStream(f);

int read = 0;
byte [] bytes = new byte [1024]; ((read = in.read(bytes))!= -1){
out.write(bytes,0,read);

while
}

urls.add(username +//+ f.getName());

System.out.println(New file created!);
catch(IOException e){
System.out.println(e.getMessage());
} finally {
try {
in.close();
out.flush();
out.close();
} catch(IOException ex){
}
}
}
// ...某些方法
}
Thank you!

解决方案

我没有尝试过GlassFish,但发现在WebLogic上使用PrimeFaces 4时,我们不得不将这个参数添加到web.xml:

 <的context-param> 
< param-name> primefaces.UPLOADER< / param-name>
< param-value> commons< / param-value>
< / context-param>

我们使用这个Maven依赖:

 <依赖性> 
< groupId> commons-fileupload< / groupId>
< artifactId> commons-fileupload< / artifactId>
< version> 1.3.1< / version>
< /依赖>

我们还必须确保 PrimeFaces FileUpload Filter 是web.xml中的第一个过滤条目。但它看起来像你已经。

此外,文件上传应该有自己的形式。


I searched and tried every example on web that i found but my example still doesn't work. My web.xml

...(headers)

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<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>
<listener>
    <listener-class>registration.cleanRegisterDB</listener-class>
</listener>
<listener>
    <listener-class>main.poolChecker</listener-class>
</listener>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<listener>
    <listener-class>Utils.SessionAtributeListener.MyAtributeListener</listener-class>
</listener>
<context-param>
    <param-name>primefaces.PRIVATE_CAPTCHA_KEY</param-name>
    <param-value>...</param-value>
</context-param>
<context-param>
    <param-name>primefaces.PUBLIC_CAPTCHA_KEY</param-name>
    <param-value>...</param-value>
</context-param>
<error-page>
    <error-code>404</error-code>
    <location>/faces/404.xhtml</location>
</error-page>

My xhtml page:

<?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">
<html 
lang="#{main_bean.authLang()}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">

<f:view locale="#{main_bean.locale}" encoding="ISO-8859-1" >
    <h:head>            
        <h:outputStylesheet library="theme1" name="css/style.css"/>
        <h:outputScript library="theme1" name="js/jquery.validate.js"/>
    </h:head>

    <h:body> 
        <f:event type="preRenderComponent" listener="#{postbean.loadMasterCategories()}" ></f:event>
        <h:panelGroup id="layout-for-image" layout="block">
            <h:link outcome="index.xhtml?faces-redirect=true">
                <h:graphicImage name="theme1/images/logo.png" />
            </h:link>
        </h:panelGroup>

        <h:panelGroup class="layout-titles" layout="block">
            <h2>#{msg.postInt}</h2>
        </h:panelGroup>
        <h:form id="formPostpool" enctype="multipart/form-data" >
            <p:growl id="growlaa" autoUpdate="true"/>
            <p:confirmDialog id="confirmDialog" message="#{msg.dialogConfPostInt}"  
                             header="#{msg.confirmDialogHeader}" severity="alert" widgetVar="confirmation" >  
                <h:panelGroup id="dialog-captcha-layout" layout="block" > 
                    <h:panelGroup layout="block" style="display: inline-block; *display: inline; zoom: 1;">
                        <p:captcha label="Captcha" validatorMessage="#{msg.invalidCaptcha}" required="true" requiredMessage="#{msg.CaptchaNull}" language="#{main_bean.authLang()}"/>
                    </h:panelGroup>
                </h:panelGroup>

                <p:commandButton id="confirm" styleClass="h-button-search" icon="iconDialogYes" actionListener="#{captchaBean.submit}"
                                 action="index" ajax="false" onclick="confirmation.hide();" style="background-image: url('../resources/theme1/images/icons/yes.png')" >
                    <f:actionListener binding="#{postbean.publicatePool()}" />
                </p:commandButton>

                <p:commandButton id="notconfirm" styleClass="h-button-search" icon="iconDialogNo"
                                 ajax="false" type="button" onclick="confirmation.hide();"/> 
            </p:confirmDialog>

            <h:panelGroup class="layout-item-details" layout="block">
                <h2>#{msg.infoProd}</h2>
                <h:panelGroup class="details-element" layout="block">
                    <h:outputLabel value="#{msg.categoryPost}" for="cat"/>
                    <h:panelGroup class="element-inputs" layout="block">
                        <p:selectOneListbox style="height: 150px;" id="cat" value="#{postbean.masterCategoria}" converter="omnifaces.SelectItemsConverter" required="true" requiredMessage="#{msg.message_radiobutton_choose}">
                            <f:selectItems value="#{postbean.categorias}" var="cat" itemLabel="#{cat.descricao}" itemValue="#{cat}" />
                        </p:selectOneListbox>  

                    </h:panelGroup>
                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block">
                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.titlePost}" for="titulo"/>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <p:inputText id="titulo" value="#{postbean.titulo}" styleClass="inputs-login" required="true" requiredMessage="#{msg.message_titulo_null}"/>
                    </h:panelGroup>
                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block">
                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.descricaoPost}" for="desc"/>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <p:inputTextarea id="desc" value="#{postbean.descricao}" rows="6" cols="70" styleClass="inputs-textarea" required="true" requiredMessage="#{msg.message_descricao_null}"/>
                    </h:panelGroup>
                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block">
                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.marcaPost}" for="marca"/>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <p:inputText id="marca" value="#{postbean.marca}" styleClass="inputs-login" />
                    </h:panelGroup>
                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block">
                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.modeloPost}" for="Modelo"/>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <p:inputText id="Modelo" value="#{postbean.modelo}" styleClass="inputs-login" />
                    </h:panelGroup>
                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block">
                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.quantidadePost}" for="qtdDesejada"/>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <p:inputText id="qtdDesejada" value="#{postbean.quantidadePretendida}" styleClass="inputs-login" style="width: 100px !important;" required="true" requiredMessage="#{msg.message_quantidade_null}" validatorMessage="#{msg.message_quantidade_invalid}">
                            <f:validateDoubleRange minimum="0.00000001" />
                            <!--<f:validateRegex pattern="([1-9][0-9]*\.?[0-9]*)" for="qtdDesejada" />-->
                        </p:inputText>
                    </h:panelGroup>
                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block">
                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.precoTargetPost}" for="precoTarget"/>
                    </h:panelGroup>
                    <h:panelGroup layout="block">
                        <p:inputText id="precoTarget" value="#{postbean.preco}" styleClass="inputs-login" style="width: 100px !important;" required="true" requiredMessage="#{msg.message_precoTarget_null}" validatorMessage="#{msg.message_precoTarget_invalid}">
                            <f:validateDoubleRange minimum="0.00000001" />
                        </p:inputText>
                    </h:panelGroup>
                </h:panelGroup>
                <h:panelGroup class="details-element" layout="block">

                    <h:panelGroup class="element-labels" layout="block">
                        <h:outputLabel value="#{msg.fotosPost}" for="foto"/>
                    </h:panelGroup>
                    <!--                        <h:panelGroup layout="block" style="display: inline-block; *display: inline; zoom: 1;" id="addFotoPanel">
                                                <h:inputFile id="idinputFile" value="#{postbean.file}" />
                                            </h:panelGroup>-->
                    <p:fileUpload value="#{postbean.upfile}" auto="true"  fileUploadListener="#{postbean.handleFileUpload}" widgetVar="asd" />

                </h:panelGroup>

                <h:panelGroup class="details-element" layout="block" style="margin-bottom: 15px; width: 250px; padding-left: 175px;">
                    <p:commandButton  id="btn" value="#{msg.label_buttonsubmit}" styleClass="h-button-search" onclick="confirmation.show();"/>
                </h:panelGroup>
            </h:panelGroup>

            <script type="text/javascript">
                                     $(document).ready(function() {
                                         $('#formPostpool').keypress(function(e) {
                                             if (e.keyCode == 13) {
                                                 event.preventDefault();
                                                 $("#btn").click();
                                             }
                                         })
                                     });
            </script>
        </h:form>
    </h:body>
</f:view>

And the bean: //...some imports

@ManagedBean(name = "postbean")
@ViewScoped
public class postbean implements Serializable {

//////////////////////////////////
//  FACADES
/////////////////////////////////
@EJB
ProdutoFacade produtofacade = new ProdutoFacade();
@EJB
UsersFacade userfacade = new UsersFacade();
@EJB
ImageFacade imagefacade = new ImageFacade();
@EJB
categoriasFacade categoriafacade = new categoriasFacade();
@EJB
buyerPoolFacade buyerPoolFacade = new buyerPoolFacade();
@EJB
LanguageFacade languageFacade = new LanguageFacade();
@EJB
Lingua_ProdutoFacade langProFacade = new Lingua_ProdutoFacade();
@EJB
MessagesFacade messagesFacade = new MessagesFacade();
/////////////////////////////////////
// VARIAVEIS
////////////////////////////////////
//Destino onde vão ser guardadas as imagens relativas a pool
private String destination = "C:";
private List<String> urls;
private String titulo;
private String descricao;
private String marca;
private String modelo;
private float preco;
private short estado;
private int quantidadePretendida;
private List<categorias> categoria; // lista das categorias
private Timestamp dataActual;
private Users publicationUser;
private categorias masterCategoria;
private categorias option2;
private List<categorias> text;
private List<categorias> subcat2;
private boolean rend;
private boolean rend2;
private Part file;
private UploadedFile upfile;

public Part getFile() {
    return file;
}

public void setFile(Part file) {
    this.file = file;
}

public UploadedFile getUpfile() {
    return upfile;
}

public void setUPfile(UploadedFile upfile) {
    this.upfile = upfile;
}

public postbean() {...}

//...getters and setters

//...some methods

public void handleFileUpload(FileUploadEvent event) {

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    FacesContext ctx = FacesContext.getCurrentInstance();
    Locale l = ctx.getViewRoot().getLocale();
    ResourceBundle rb = ResourceBundle.getBundle("languages.welcome", l);

    FacesContext aFacesContext = FacesContext.getCurrentInstance();
    ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
    try {
        copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        FacesMessage msg = new FacesMessage(rb.getString("Success"), event.getFile().getFileName() + rb.getString("wasUploaded"));
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void copyFile(String fileName, InputStream in) {
    String username = userfacade.findUserByIDSessao(FacesUtils.getSessionID());
    OutputStream out = null;
    try {
        // write the inputStream to a FileOutputStream
        File dir = new File(destination + username);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File f = new File(dir + "\\" + fileName);
        out = new FileOutputStream(f);

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

        urls.add(username + "//" + f.getName());

        System.out.println("New file created!");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            in.close();
            out.flush();
            out.close();
        } catch (IOException ex) {
        }
    }
}
//..some methods
}

Well, I changed parameters, I removed the filter from web.xml file, I changed the bean... I know I've got to have a "multipart/form-data" form, a filter in web.xml file and a fileUploader to receive the event in the bean. However nothing is triggered. I click to choose and then to upload (if parameter is not auto="true") and nothing happens. I do have commons-io-2.4, commons-fileupload-1.3, primefaces-3.5 and I use GlassFish Server 4. Thank you!

解决方案

I haven't tried GlassFish but found that using PrimeFaces 4 on WebLogic we had to add this parameter to the web.xml:

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>commons</param-value>
</context-param>

We're using this Maven dependency:

<dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3.1</version>
</dependency>

We also had to make sure that the PrimeFaces FileUpload Filter was the first filter entry in the web.xml. But it looks like yours is already.

Also, the file upload should have a form of its own.

这篇关于JSF - Primefaces FileUpload不触发方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆