Jsf 未调用PrimeFaces文件下载方法?

Jsf 未调用PrimeFaces文件下载方法?,jsf,primefaces,bytearray,download,Jsf,Primefaces,Bytearray,Download,我有多个页面,允许下载相同的资源(从我的数据库检索) 问题是,下载只在其中一些上起作用,即使使用相同的代码和调用相同的bean 这件事变得很烦人,因为在非工作页面上,单击下载链接只会重新加载页面,没有任何消息/异常,所以我无法了解发生了什么 以下是我的BEAN代码: package ManagedBeans; import ejb.DispensaManagerLocal; import entity.Dispensa; import entity.Utente; import java.io

我有多个页面,允许下载相同的资源(从我的数据库检索)

问题是,下载只在其中一些上起作用,即使使用相同的代码和调用相同的bean

这件事变得很烦人,因为在非工作页面上,单击下载链接只会重新加载页面,没有任何消息/异常,所以我无法了解发生了什么

以下是我的BEAN代码:

package ManagedBeans;

import ejb.DispensaManagerLocal;
import entity.Dispensa;
import entity.Utente;
import java.io.ByteArrayInputStream;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RateEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author stefano
 */
@ManagedBean
@RequestScoped
public class DispensaBean {

    @EJB
    private DispensaManagerLocal dispensaManager;
    @ManagedProperty(value = "#{loginBean.utente}")
    private Utente utente;

    public Utente getUtente() {
        return utente;
    }

    public void setUtente(Utente utente) {
        this.utente = utente;
    }

    /**
     * Creates a new instance of DispensaBean
     */
    public DispensaBean() {
    }

    public StreamedContent getDownload() {
        String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("dispensaId");        
        System.out.println("________" + id);
        Dispensa d = dispensaManager.findById(Integer.parseInt(id));        
        String type = getMimeFromByte(d.getDatiFile());
        String estensione = "";
        if(type.equals("application/pdf")){
            estensione = ".pdf";
        } else if(type.equals("application/zip")) {
            estensione = ".zip";
        } else if(type.equals("application/vnd.ms-powerpoint")) {
            estensione = ".ppt";
        }        
        return new DefaultStreamedContent(new ByteArrayInputStream(d.getDatiFile()), type, d.getTitolo() + estensione);
    }


    private String getMimeFromByte(byte[] src) {
        if (src[0] == 0x25 && src[1] == 0x50 && src[2] == 0x44 && src[3] == 0x46) {
            return "application/pdf";
        }
        if (src[0] == 0x50 && src[1] == 0x4b) {
            return "application/zip";
        }
        if (src[0] == 0xd0 && src[1] == 0xcf && src[2] == 0x11 && src[3] == 0xe0 && src[4] == 0xa1 && src[5] == 0xb1 && src[6] == 0x1a && src[7] == 0xe1) {
            return "application/vnd.ms-powerpoint";
        }
        return "application/octet-stream";
    }

}
现在,在非工作页面上,不会调用
getDownload()
方法,因为它不会打印任何内容

这是下载按钮代码

<h:form style="float: right">
    <pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}">
       <pou:graphicImage value="./resources/images/download.png" height="30"/>
       <pou:fileDownload value="#{dispensaBean.getDownload()}"/>                                                    
       <f:param name="dispensaId" value="#{dispensa.id}"/>
    </pou:commandLink>                                
</h:form> 
我不知道该怎么办,因为它没有说出哪里出了问题,只是绕过我的下载重新加载了页面

编辑:


我已尝试更改我的
getDownload()
方法,以返回我硬盘上的
文件
,以了解问题是否是由数据库上的空数据引起的,但它仍然不能像我所说的那样工作

我似乎通过使用另一种解决方案解决了这个问题

我改变了一切

<h:form style="float: right">
        <pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}">
           <pou:graphicImage value="./resources/images/download.png" height="30"/>
           <pou:fileDownload value="#{dispensaBean.getDownload()}"/>                                                    
           <f:param name="dispensaId" value="#{dispensa.id}"/>
        </pou:commandLink>                                
    </h:form> 

因此,它加载下载页面,自动单击下载链接,并在显示下载对话框时自动关闭页面。

我也遇到过同样的问题。我对它进行了调试,发现表单中有表单,因为我在另一个模板中包含了模板,因为它是一个摘要屏幕。因此,我删除了内部模板中的所有h:form标记,但根xhtml页面除外,该页面包含所有这些模板,并且可以正常工作。

您应该将解决方案作为答案发布,而不是发布在问题中(并没有写在标题中,但只是将其标记为已接受)。Stack Overflow是一个问答网站,而不是一个讨论论坛。从没想过我可以接受自己的答案!你只会因此得不到分数:)
<h:form style="float: right">
        <pou:commandLink id="downloadDispensa" ajax="false" disabled="#{!loginBean.logged}">
           <pou:graphicImage value="./resources/images/download.png" height="30"/>
           <pou:fileDownload value="#{dispensaBean.getDownload()}"/>                                                    
           <f:param name="dispensaId" value="#{dispensa.id}"/>
        </pou:commandLink>                                
    </h:form> 
<h:form style="float: right">
    <h:outputLink id="downloadDispensa" disabled="#{!loginBean.logged}" target="_blank" value="./download.xhtml?id=#{dispensa.id}">
         <pou:graphicImage value="./resources/images/download.png" height="30"/>                                    
     </h:outputLink>                                
</h:form>
<script type="text/javascript">
    if(document.referrer == "" || document.referrer == "download.xhtml"){
        self.location='./index.xhtml';
    }
    document.onblur = new Function('self.close()');
</script>
<h:body onload="document.getElementsByClassName('downloadDispensa')[0].click();" rendered="#{loginBean.logged}">
    <h:form>            
        <h:commandLink class="downloadDispensa" id="downloadDispensa" style="display: none">                
            <pou:graphicImage value="./resources/images/download.png" height="30"/>
            <pou:fileDownload value="#{dispensaBean.download}"/>                                                                                       
            <f:param name="dispensaId" value="#{request.getParameter('id')}"/>
        </h:commandLink> 
    </h:form>        
</h:body>
<h:body onload="self.location='./index.xhtml';" rendered="#{!loginBean.logged}">
</h:body>