org.primefaces.component.filedownload.FileDownloadActionListener.processAction上的java.lang.NullPointerException [英] java.lang.NullPointerException at org.primefaces.component.filedownload.FileDownloadActionListener.processAction

查看:351
本文介绍了org.primefaces.component.filedownload.FileDownloadActionListener.processAction上的java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个JSF / PrimeFaces项目,我从数据库生成一些文件,并将它们放在我的项目的web / resources文件夹中。我使用ap:datable将文件名动态地加载到一个网页。数据表启用了单个模式。我想要的是根据用户选择使文件可以下载。当选择一个文件并按下下载按钮抛出异常时;



以下是我使用的代码:



JSF页面



 < div id =content> 
< h:form id =resultForm>
< center>
< p:panel id =filesstyle =width:80%>
< p:dataTable id =resultDTvar =filevalue =#{gen.outFileNames}
selection =#{downloader.selectedFileName}selectionMode =singlerowKey = #{文件} >
< f:facet name =header>
生成的文件
< / f:facet>
< p:column headerText =文件名>
< h:outputText value =#{file}/>
< / p:column>
< f:facet name =footer>
< p:commandButton value =下载ajax =falseicon =ui-icon-arrowthick-1-s>
< p:fileDownload value =#{downloader.file}/>
< / p:commandButton>
< / f:facet>
< / p:dataTable>
< / p:panel>
< / center>
< / h:form>
< / div>



下载控制器



 code> @ManagedBean(name =downloader)
@ViewScoped
public class FilesDownloadController implements Serializable {

private String selectedFileName;

public StreamedContent getFile(){
if(selectedFileName == null){
FacesContext.getCurrentInstance()。addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,
下载警告,请点击文件并点击下载按钮。));
返回null;
}
System.out.println(下载1:selectedFileName =+ selectedFileName);
System.out.println(下载2);

System.out.println(下载3);
InputStream stream = this.getClass()。getResourceAsStream(selectedFileName);
System.out.println(下载4);
StreamedContent file = new DefaultStreamedContent(stream,((ServletContext)FacesContext.getCurrentInstance()。getExternalContext()。getContext())。getMimeType(selectedFileName),
selectedFileName.split(/)[selectedFileName .split(/)。length - 1]);
System.out.println(Download 5 =+ selectedFileName.split(/)[selectedFileName.split(/)。length - 1]);
System.out.println(file!= null?文件内容OK+ file.getContentType()++ file.getName():文件内容NOK);
返回文件;
}

public String getSelectedFileName(){
return selectedFileName;
}

public void setSelectedFileName(String selectedFileName){
this.selectedFileName = selectedFileName;
System.out.println(Selected File changed:+ this.selectedFileName);
}
}



生成器bean



  @ManagedBean(name =gen)
@SessionScoped
public class GeneratorController implements Serializable {

private List< ;字符串> outFileNames;

public GeneratorController(){
outFileNames = new ArrayList< String>();
outFileNames.add(/ resources / images / optimus.jpg);
outFileNames.add(/ resources / images / optimus_1.jpg);
outFileNames.add(/ resources / images / optimus_2.jpg);
outFileNames.add(/ resources / images / optimus_3.jpg);
}

public List< String> getOutFileNames(){
return outFileNames;
}

public void setOutFileNames(List< String> outFileNames){
this.outFileNames = outFileNames;
}
}



抛出异常



 信息:所选文件已更改:/resources/images/optimus_1.jpg 
信息:下载1:selectedFileName = /resources/images/optimus_1.jpg
信息:下载2
信息:下载3
信息:下载4
信息:下载5 = optimus_1.jpg
信息:文件内容OK image / jpeg optimus_1.jpg
FATAL:JSF1073:javax.faces.FacesExceptioninterceptédurant le traitement de INVOKE_APPLICATION 5:UIComponent-ClientId =,Message = null
FATAL:没有关联的消息
javax.faces.FacesException
在com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
在com.sun .faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
在javax.faces.webapp.FacesSe rvlet.service(FacesServlet.java:646)
在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
在org.apache.catalina.core.StandardWrapperValve.invoke( StandardWrapperValve.java:318)
在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java: 734)
在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
在org .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
在com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459)
在com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
在org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
在org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
在org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)
在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
在org.glassfish。 grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorE xecutor.java:77)
在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy。 java:112)
在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56 )
在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:565 )
在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:545)
在java.lang.Thread.run(Thread.java:745)
导致通过:java.lang.NullPointerException
在org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDow nloadActionListener.java:81)
在javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
在javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
在javax.faces.component.UIData.broadcast(UIData.java:1108)
在javax.faces.component .UIViewRoot.broadcastEvents(UIViewRoot.java:790)
在javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
在com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase .java:81)
... 31更多


解决方案

我终于有了一种下载文件的方法。图像所在的文件夹不在类路径上,所以

  InputStream stream = this.getClass()。getResourceAsStream( selectedFileName); 

返回 null ,发生NPE。所以通过用

   

webapp上下文用于查找文件,它的工作原理


I am working on a JSF/PrimeFaces project where I am generating some files from a database and placing them in the web/resources folder of my project.I am using a p:datable to load the file names dynamically into a web page. The datatable has a singleMode activated. What I want is to make the files downloadable based on the users choice.when a file is selected and the download button is pushed an exception is thrown;

Here are the codes I used:

JSF page

   <div id="content">
        <h:form id="resultForm">
            <center>
                <p:panel id="files" style="width: 80%">
                    <p:dataTable id="resultDT" var="file" value="#{gen.outFileNames}" 
                                 selection="#{downloader.selectedFileName}" selectionMode="single" rowKey="#{file}">
                        <f:facet name="header">
                            Generated Files
                        </f:facet>
                        <p:column headerText="File names">
                            <h:outputText value="#{file}" />
                        </p:column>
                        <f:facet name="footer">
                            <p:commandButton value="Download" ajax="false" icon="ui-icon-arrowthick-1-s">
                                <p:fileDownload value="#{downloader.file}"/>
                            </p:commandButton>
                        </f:facet>
                    </p:dataTable>
                </p:panel>
            </center>
        </h:form>
    </div>

Download Controller

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

    private String selectedFileName;

    public StreamedContent getFile() {
        if (selectedFileName == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Download warning", "Please click a file and click on the download button."));
            return null;
        }
        System.out.println("Download 1 : selectedFileName = " + selectedFileName);
        System.out.println("Download 2");

        System.out.println("Download 3");
        InputStream stream = this.getClass().getResourceAsStream(selectedFileName);
        System.out.println("Download 4");
        StreamedContent file = new DefaultStreamedContent(stream, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getMimeType(selectedFileName), 
        selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
        System.out.println("Download 5 = " + selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
        System.out.println(file != null ? "File content OK " + file.getContentType() + " " + file.getName() : "File content NOK");
        return file;
    }

    public String getSelectedFileName() {
        return selectedFileName;
    }

    public void setSelectedFileName(String selectedFileName) {
        this.selectedFileName = selectedFileName;
        System.out.println("Selected File changed: " + this.selectedFileName );
    }
}

Generator bean

@ManagedBean(name = "gen")
@SessionScoped
public class GeneratorController implements Serializable {

private List<String> outFileNames;

public GeneratorController() {
    outFileNames = new ArrayList<String>();
    outFileNames.add("/resources/images/optimus.jpg");
    outFileNames.add("/resources/images/optimus_1.jpg");
    outFileNames.add("/resources/images/optimus_2.jpg");
    outFileNames.add("/resources/images/optimus_3.jpg");
}

public List<String> getOutFileNames() {
    return outFileNames;
}

public void setOutFileNames(List<String> outFileNames) {
    this.outFileNames = outFileNames;
}
}

Exceptions thrown

        Infos:   Selected File changed: /resources/images/optimus_1.jpg
        Infos:   Download 1 : selectedFileName = /resources/images/optimus_1.jpg
        Infos:   Download 2
        Infos:   Download 3
        Infos:   Download 4
        Infos:   Download 5 = optimus_1.jpg
        Infos:   File content OK image/jpeg optimus_1.jpg
        FATAL:   JSF1073 : javax.faces.FacesException intercepté durant le      traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=null
        FATAL:   No associated message
javax.faces.FacesException
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
       at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
       at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
       at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
       at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
       at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
       at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
       at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
       at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
       at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:81)
       at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
       at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
       at javax.faces.component.UICommand.broadcast(UICommand.java:300)
       at javax.faces.component.UIData.broadcast(UIData.java:1108)
       at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
       at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    ... 31 more

解决方案

I finally got a way to download the files. The folder where the images are located is not on the class path, so

InputStream stream = this.getClass().getResourceAsStream(selectedFileName);

returns null and an NPE occurs. So by replacing the code of the Download controller with

InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(selectedFileName);

The webapp context is used for locating the files and it works

这篇关于org.primefaces.component.filedownload.FileDownloadActionListener.processAction上的java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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