是否可以通过JSF确定方法调用的执行顺序?

时间:2016-03-11 08:08:34

标签: javascript jsf

我的问题是,后台bean方法#{infomappeController.sendInfomappeAsMail}会立即被调用,并且不会等待javascript函数createpdfresource()返回正确的参数(应该是一个url)。

我的问题:

  • 你怎么能保证我的支持方法等到javascript方法结束?
  • 你能通过JSF做到吗?

xhtml文件(代码段):

<!-- TODO: backing bean method-call doesn't wait for javascript function to finish which as a result returns an incomplete url -->                     
<h:commandButton class="sendInfoMapAsEmail noMarginLeft"
                 id="sendInfoMapAsEmail"
                 value="Als Email versenden"
                 actionListener="#{infomappeController.sendInfomappeAsMail}">   
    <f:ajax render=":emailStatusMessage" />                                 
</h:commandButton>      

javascript方法:

$(".sendInfoMapAsEmail").click(createpdfresource);
function createpdfresource(e){
    e.preventDefault();     
    var cleanurl = $('.cleanurl').attr('href');
    var clientId = $('.clientId').val();
    var formId = "#"+clientId+"\\:infofolderForm";
    var seralizedform = $(formId).serialize();
    var pdfurl = cleanurl + "&" + seralizedform + "&contentType=pdf";
    var hiddenResource = formId + '\\:pdfresource';
    $(hiddenResource).val(pdfurl);       
}  

支持bean方法:

public void sendInfomappeAsMail() {
    //Mailer mailer = new Mailer();     
    FacesContext context = FacesContext.getCurrentInstance();
    String beraterEmail="";
    try {
        beraterEmail = themeDisplay.getRealUser().getEmailAddress();
    } catch(Exception e){
        logger.error("sendInfomappeAsMail: no EmailAdress for "+themeDisplay.getRealUser(),e);
    }

    mailer.sendMail(beraterEmail, pdfresource);

    if(mailer.isWasEmailSentSuccessfully()) {
        FacesMessage successMessage = new FacesMessage(FacesMessage.SEVERITY_INFO,"Infomappe erfolgreich versendet.", null);
        context.addMessage(null, successMessage);
    } else {
        FacesMessage errorMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Fehler beim Versenden der Infomappe.", null);
        context.addMessage(null, errorMessage);

    }
}

非常感谢你的帮助。

0 个答案:

没有答案