本文介紹了Java Wicket AjaxLink和RequestHandler-如何?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
使用場景:用戶點擊網站上的一個鏈接,就會得到動態生成的德語和英語兩種語言的PDF。
此代碼可以工作。GenerateCouponPdfOnClick創建包含PDF文件的ResourceStreamRequestHandler。
Link<Void> generatePdf = new Link<Void>("generatePdf") {
@Override
public void onClick() {
ResourceStreamRequestHandler requestHandler = generateCouponPdfOnClick(coupon, PDFLanguage.GERMAN);
getRequestCycle().scheduleRequestHandlerAfterCurrent(requestHandler);
}
};
現在我想更改代碼以獲得彈出窗口(Wicket ModalWindow,這里的類為PdfDialogPage),用戶可以通過點擊兩個按鈕中的一個來選擇PDF的語言(德語/英語)。如果用戶單擊按鈕,則應該關閉ModalWindow,并且應該生成并顯示PDF。
以下是修改后的代碼:變量pdflanguageanguage包含選定的語言,由PdfDialogPage使用PageReferenz設置
AjaxLink<Void> generatePdf = new AjaxLink<Void>("generatePdf") {
@Override
public void onClick(final AjaxRequestTarget target) {
modalWindow.setPageCreator(() -> {
return new PdfDialogPage(getPage().getPageReference(), modalWindow);
});
modalWindow.setWindowClosedCallback((AjaxRequestTarget target1) -> {
ResourceStreamRequestHandler requestHandler = generateCouponPdfOnClick(coupon, pdflanguageanguage);
getRequestCycle().scheduleRequestHandlerAfterCurrent(requestHandler);
});
modalWindow.show(target);
};
當我單擊該鏈接時,將打開modal窗口。我選擇語言,例如英語,在將”English”寫入變量pdflanguageanguage時,modalWindow關閉。總比什么都沒發生要好。并且不會生成該PDF。Wicket AJAX調試窗口告訴我以下日志:
</head>]]></header-contribution><evaluate><![CDATA[(function(){var settings = {"minWidth":200,"minHeight":200,"className":"w_blue","width":400,"height":100,"resizable":true,"src":"./wicket/page?6","mask":"semi-transparent","autoSize":false,"unloadConfirmation":true,"onClose":function() { Wicket.Ajax.ajax({"u":"./overviewCoupons?5-1.1-panel-modalWindow","c":"modalWindowb0"}); },"onCloseButton":function() { Wicket.Ajax.ajax({"u":"./overviewCoupons?5-1.0-panel-modalWindow","c":"modalWindowb0"});; return false; }};window.setTimeout(function(){
Wicket.Window.create(settings).show();
}, 0);
})();]]></evaluate></ajax-response>
INFO: returned focused element: javascript:;
INFO: returned focused element: javascript:;
INFO: Response processed successfully.
INFO: refocus last focused component not needed/allowed
INFO: focus removed from generatePdf5
INFO: focus set on _wicket_window_3
INFO: focus set on undefined
INFO: focus removed from undefined
ERROR: Wicket.Ajax.Call.failure: Error while parsing response: Error: Invalid XML: %PDF-1.4 %????
1 0 obj
<<
/Creator (Apache FOP Version 2.3)
/Producer (Apache FOP Version 2.3)
/CreationDate (D:20180808160110+02'00')
我認為AJAX和”getRequestCycle().scheduleRequestHandlerAfterCurrent(requestHandler);”.之間存在問題我如何將它們一起使用。因此我將代碼減少為:
AjaxLink<Void> generatePdf = new AjaxLink<Void>("generatePdf") {
@Override
public void onClick(final AjaxRequestTarget target) {
ResourceStreamRequestHandler requestHandler = generateCouponPdfOnClick(coupon, PDFLanguage.ENGLISH);
getRequestCycle().scheduleRequestHandlerAfterCurrent(requestHandler);
}
};
當我知道單擊AjaxLink時,什么也不會發生,但我得到相同的AJAX日志錯誤輸出。
如何將AjaxLink與ResourceStreamRequestHandler一起使用?
推薦答案
我用這種方式解決了一個與您類似的用例。
您需要here中的Java類AjaxDownload
或者,您也可以使用My Variant,它接受FileResourceStream
作為輸入。
import lombok.extern.slf4j.Slf4j;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
import org.apache.wicket.request.resource.ContentDisposition;
import org.apache.wicket.util.resource.FileResourceStream;
/**
* @author Sven Meier
* @author Ernesto Reinaldo Barreiro (reiern70@gmail.com)
* @author Jordi Deu-Pons (jordi@jordeu.net)
*/
@Slf4j
public class AJAXDownload extends AbstractAjaxBehavior {
private boolean addAntiCache;
private FileResourceStream fileResourceStream;
private String fileName;
public AJAXDownload() {
this(true);
}
public AJAXDownload(boolean addAntiCache) {
super();
this.addAntiCache = addAntiCache;
}
/**
* Call this method to initiate the download.
*/
public void initiate(AjaxRequestTarget target, FileResourceStream fileResourceStream, String fileName) {
this.fileResourceStream= fileResourceStream;
this.fileName = fileName;
String url = getCallbackUrl().toString();
if (addAntiCache) {
url = url + (url.contains("?") ? "&" : "?");
url = url + "antiCache=" + System.currentTimeMillis();
}
// the timeout is needed to let Wicket release the channel
target.appendJavaScript("setTimeout("window.location.href='" + url + "'", 100);");
}
public void onRequest() {
try{
ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(fileResourceStream, fileName);
handler.setContentDisposition(ContentDisposition.ATTACHMENT);
getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
} catch (Exception e){
log.error("Error while trying to download", e);
onErrorOccurred();
}
}
protected void onErrorOccurred(){}
}
如何使用?
您必須將AjaxDownload添加到您的表單
form.add(ajaxDownload = new AJAXDownload());
在通過AjaxLink提交的onSubmit中,您可以調用:
File yourPdfFile = //create your pdf file;
final FileResourceStream resourceStream= new FileResourceStream(yourPdfFile);
ajaxDownload.initiate(target, resourceStream, "yourPDF.pdf");
這篇關于Java Wicket AjaxLink和RequestHandler-如何?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,