如何在 Wicket 中启动文件下载并关闭 (jQuery) 对话框?
在 Wicket 应用程序中,我有一个模式对话框,其中包含一个简单的表单和一个按钮。用户输入值(报告参数),然后单击开始下载报告文件(通常为 PDF)的按钮。 (所有表单值都是必需的,Wicket 的验证机制用于确保用户在下载开始之前输入它们。)
也许用图片可以更好地解释这一点:
我在这里使用 jQuery UI 对话框(而不是Wicket 的 ModalWindow 从用户的角度来看,感觉更加笨拙和丑陋)。
除了单击下载按钮时/之后关闭对话框之外,一切都非常正常。
当前版本(省略不相关的部分):
public class ReportDownloadLink extends Link {
public ReportDownloadLink(String id, ReportDto report) {
super(id);
this.report = report;
}
@Override
public void onClick() {
IResourceStream resourceStream = new AbstractResourceStreamWriter() {
@Override
public void write(OutputStream output) {
try {
reportService.generateReport(output, report);
} catch (ReportGenerationException e) {
// ...
}
}
@Override
public String getContentType() {
// ...
}
};
ResourceStreamRequestTarget target =
new ResourceStreamRequestTarget(resourceStream, report.getFileName());
getRequestCycle().setRequestTarget(target);
}
该对话框是一个 Wicket 面板(它使用上面的 ReportDownloadLink),我们将其放入某个 div 中,然后当在列表中选择报表时,从 AjaxLink 的 onClick 打开该对话框() 非常简单,如下所示:
target.appendJavascript(String.format("showReportExportDialog('%s')", ... ));
调用此 JS 函数:
function showReportExportDialog(dialogTitle) {
$("#reportExportPanelContainer").dialog(
{modal:true, draggable:true, width: 320, height: 330, title: dialogTitle}
);
}
一些选项:
- 让 ReportDownloadLink 扩展 Link 之外的其他内容,也许,和/或找到一个适当的方法来覆盖,这将允许我执行所需的一小部分 JavaScript关闭 jQuery 对话框。
- 研究 jQuery + Wicket 库(例如 jqwicket 或 wiquery)据说可以让这两者更好地协同工作。
我最近尝试的是重写方法 getOnClickScript() 在 ReportDownloadLink 中,这似乎很有前途(根据 Javadocs,它返回“任何应该是的 onClick JavaScript used"):
@Override
protected CharSequence getOnClickScript(CharSequence url) {
return "closeDownloadDialog()";
}
问题是,这会导致 onClick() 根本不会被调用,即下载不会开始。
我是否可以重写 Wicket(比 Link)更多的“ajaxy”类来组合这些内容:首先初始化下载,然后调用 JS 来关闭对话框?
类似案例有什么建议或经验吗?请注意,我想在这里继续使用 jQuery 对话框,尽管它使诸如此类的事情变得更加复杂。使用下载链接(查看相关问题< /a>) 也很好,以防让事情变得更容易。
注意:如果您推荐 JQWicket 或 wiQuery,请提供如何执行此操作的示例。
In a Wicket app, I have a modal dialog that contains a simple form and a button. User enters values (report parameters), and then clicks the button which starts the download of a report file (typically a PDF). (All form values are required, and Wicket's validation mechanism is used to make sure user entered them before the download can start.)
Maybe this is better explained with a picture:
I'm using here a jQuery UI Dialog (instead of Wicket's ModalWindow which felt a lot clumsier and uglier from user's perspective).
Everything is pretty much working, except closing the dialog when/after clicking the download button.
Current version (irrelevant bits omitted):
public class ReportDownloadLink extends Link {
public ReportDownloadLink(String id, ReportDto report) {
super(id);
this.report = report;
}
@Override
public void onClick() {
IResourceStream resourceStream = new AbstractResourceStreamWriter() {
@Override
public void write(OutputStream output) {
try {
reportService.generateReport(output, report);
} catch (ReportGenerationException e) {
// ...
}
}
@Override
public String getContentType() {
// ...
}
};
ResourceStreamRequestTarget target =
new ResourceStreamRequestTarget(resourceStream, report.getFileName());
getRequestCycle().setRequestTarget(target);
}
The dialog is a Wicket Panel (which makes use of ReportDownloadLink above), which we put in a certain div, and then when a report is selected in a list, the dialog is opened from an AjaxLink's onClick() quite simply like this:
target.appendJavascript(String.format("showReportExportDialog('%s')", ... ));
Which calls this JS function:
function showReportExportDialog(dialogTitle) {
$("#reportExportPanelContainer").dialog(
{modal:true, draggable:true, width: 320, height: 330, title: dialogTitle}
);
}
Some options:
- Make ReportDownloadLink extend something else than Link, perhaps, and/or find an appropriate method to override which would allow me to execute the tiny bit of JavaScript needed to close the jQuery Dialog.
- Investigate jQuery + Wicket libraries (such as jqwicket or wiquery) that supposedly make these two work better together.
Latest thing I tried was overriding method getOnClickScript() in ReportDownloadLink which seemed promising (according to the Javadocs, it returns "Any onClick JavaScript that should be used"):
@Override
protected CharSequence getOnClickScript(CharSequence url) {
return "closeDownloadDialog()";
}
Thing is, this causes onClick() not to be called at all, i.e., the download doesn't start.
Could I perhaps override some more "ajaxy" class from Wicket (than Link) to combine these things: first init the download, then call the JS for closing the dialog?
Any recommendations or experiences from similar cases? Note that I want to keep using the jQuery dialog here, even though it makes things like these more complicated. Using a DownloadLink (see related question) is fine too in case that makes things easier.
NB: if you recommend JQWicket or wiQuery, please provide an example of how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以尝试仅使用 JQuery 将关闭模式代码绑定到按钮“click”事件,在模式面板页面中添加类似于
${"#mySubmit").click(myCloseModalFunction)
。它应该保留 Wicket 默认行为并在组合中添加模式关闭。另一种方法是重写
getOnClickScript(...)
方法,但 JavaScript 必须返回 true,以便浏览器调用继续链接评估并加载相应的 href。如果返回 false,则评估停止。我会建议类似的东西希望它有帮助......
Maybe you can try to bind the close modal code to the button "click" event using only JQuery, in your modal panel page, add something similar to
${"#mySubmit").click(myCloseModalFunction)
. It should keep Wicket default's behavior and add modal closing in the mix.The other way is to override the
getOnClickScript(...)
method but the javascript has to return true in order for the browser to call the continue link evaluation and load the corresponding href. If you return false, the evaluation stops. I would suggest something likeHope it helps...
请参阅 https://cwiki.apache。 org/WICKET/ajax-update-and-file-download-in-one-blow.html 获取灵感。
See https://cwiki.apache.org/WICKET/ajax-update-and-file-download-in-one-blow.html for inspiration.