我的问题是,后台bean方法#{infomappeController.sendInfomappeAsMail}
会立即被调用,并且不会等待javascript函数createpdfresource()
返回正确的参数(应该是一个url)。
我的问题:
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);
}
}
非常感谢你的帮助。