p:commandLink not invoked when placed in p:dataTab

2019-09-07 08:30发布

问题:

This question already has an answer here:

  • commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated 11 answers

I use primefaces and I want to download a file with different formats(pdf, jpg, png, ) from database but I don't succed to realise this I have seen a lot of topic like this but their methods don't work for me here is the html :

<ui:repeat value="#{histCommController.selectedCommande.listFichiers}" var="jjjjj">
                            <tr>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.nom}" />
                                </td>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.taille}" />
                                </td>
                                <td>
                                    <p:commandButton id="downloadLink" value="Download" ajax="false"   
                                                     icon="ui-icon-arrowthichk-s">  
                                        <p:fileDownload value="#{jjjjj.convertFichieru}" /> 
                                    </p:commandButton>
                                </td>
                            </tr>



                        </ui:repeat>

and here is the java :

 public StreamedContent convertFichier(byte[] bytes) {
    InputStream is = new ByteArrayInputStream(bytes);
    System.out.println("size file : "+bytes.length);
    StreamedContent image = new DefaultStreamedContent(is);
    System.out.println("dans le convertisseur : "+image.getContentType());
    return image;
}

always, image.getContentType() return null and bytes.length not null

do you have any idea

thank you


I just know the problem, I put the download link in dialog box, because when i make a test outside the dialog it works here is the test that it works :

 <h:form>

            <p:commandLink id="downloadLink" value="Download" ajax="false">  
                <p:fileDownload value="#{histCommController.test}" />  
            </p:commandLink> 
            </h:form>

and here is the test inside the dialog :

<p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" id="carDlg"  
                          showEffect="fade" hideEffect="explode" modal="true">  

                    <table id="gradient-style" >
                        <tr style="border: hidden;">
                            <th>
                                <h:outputLabel value="Model:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.id}" />
                            </td>                                       
                        </tr>
                        <tr style="border: hidden;">
                            <th>
                                <h:outputLabel value="Year:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.etat}" />
                            </td>                                       
                        </tr>
                        <tr style="border: hidden;">
                            <th>
                                <h:outputLabel value="Manufacturer:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.dateEnvoi}" />
                            </td>                                       
                        </tr>
                        <tr style="border: hidden;">
                            <th>

                                <h:outputLabel value="Color:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.dateLivraisonRecommande}" />
                            </td>                                       
                        </tr>
                    </table>
                    <table id="gradient-style" >
                        <th>Nom Fichier</th><th>Taille</th><th>Télécharger</th>
                        <ui:repeat value="#{histCommController.selectedCommande.listFichiers}" var="jjjjj">
                            <tr>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.nom}" />
                                </td>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.taille}" />
                                </td>
                                <td>
                                    <p:commandLink id="downloadLink" value="Download" ajax="false">  
                                        <p:fileDownload value="#{histCommController.test}" />  
                                    </p:commandLink>
                                </td>
                            </tr>



                        </ui:repeat>

                    </table>


                </p:dialog>  

this latter doesn't work

do you any idea how to make working the download link inside the dialog

thank you in advance

回答1:

You should set the content type yourself. Take a look at the constructors of DefaultStreamedContent. So instead of

StreamedContent image = new DefaultStreamedContent(is);

You should write

StreamedContent image = new DefaultStreamedContent(is, "image/jpeg", "fileName.jpg");

Make sure you use the proper content type, and extension for filename.