【问题标题】:Wicket - Display a PDF in a new windowWicket - 在新窗口中显示 PDF
【发布时间】:2013-11-05 09:49:08
【问题描述】:

使用 Wicket 1.5,我需要在新窗口中显示 PDF 文件。为此,我扩展了 ByteArrayResource。问题是内容处理只有两个选项:附件和内联。

第一个下载文件,当我的用户只想查阅文件时,这样做太长了。 第二个在同一个窗口中打开文件,用户希望一个新窗口继续处理打开的文档。

这是我的 ByteArrayResource 代码:

public class FileArchiveRessource extends ByteArrayResource {

    private final String genericName;
    private final Locale locale;

    public FileArchiveRessource(String genericName, Locale locale) {
        super("application/pdf");
        this.genericName = genericName;
        this.locale = locale;
        this.contentType = "application/pdf";
    }

    @Override
    protected byte[] getData(IResource.Attributes attributes) {
        try {
            String name = genericName.replaceAll("[{][$][}]", locale.getLanguage().toUpperCase());
            return IOUtils.toByteArray(this.getClass().getResourceAsStream(name));
        } catch (IOException ex) {
            return new byte[0];
        }
    }

    @Override
    protected void configureResponse(ResourceResponse response, Attributes attributes) {
        super.configureResponse(response, attributes);
        response.setContentDisposition(ContentDisposition.INLINE);
    }
}

在此实现中,名称中的 {$} 将替换为用户的语言。

资源是如何实例化的:

final Locale locale = getLocale();
final String name = "document-{$}.pdf".replaceAll("[{][$][}]", locale.getLanguage().toUpperCase());
ResourceLink button = new ResourceLink("list.openbutton", new ResourceReference(name) {
    @Override
    public IResource getResource() {
        return new FileArchiveRessource(name, locale);
    }
});

add(button);

这是我的资源的打开按钮的示例:

<input wicket:id="list.openbutton"
       type="submit"
       class="button openbutton"
       value="Open" title="Open resource" />

有什么建议吗?

精度: 我真的需要一个新的单独窗口,而不是一个新标签。

【问题讨论】:

    标签: java html pdf wicket wicket-1.5


    【解决方案1】:

    为什么不只是在 HTML 中使用简单的链接而不是按钮?

    <a href="#" target="_blank" wicket:id="list.openbutton">Open resource</a>
    

    【讨论】:

    • 这不是我所需要的,因为我真的需要一个新窗口,而不是一个新标签。但多亏了你,我找到了一个使用 Javascript 的好解决方案。看我的回答。
    【解决方案2】:

    感谢 Joachim,我找到了完全符合我需求的解决方案。我使用 javascript 打开一个包含文档的新窗口。

    这里是javascript:

    function openPDF(source, name) {
        var screenHeight = screen.height;
        var height = screenHeight - 100;
        var width = height / (1.3);
    
        var specs = 'directories=0, location=0, toolbar=0, menubar=0, resizable=1, status=0';
        specs += ', height=' + height;
        specs += ', width=' + width;
        specs += ', left=' + (screen.width - width)/2;
        specs += ', top=' + 10;
    
        window.open(source, name, specs);
        return false;
    }
    

    还有按钮:

    <a href="#" target="_blank" wicket:id="list.openbutton"
            class="button openbutton" onClick="return openPDF(this.href, 'PDF file')">Open</a>
    

    【讨论】:

      【解决方案3】:

      尝试将onclick="target='_blank';return true;" 添加到您的输入中

      【讨论】:

        【解决方案4】:

        我认为最好的解决方案是使用这个: 这将打开一个带有弹出配置的新窗口。

        PopupSettings popupSettings = new PopupSettings(PopupSettings.RESIZABLE | PopupSettings.SCROLLBARS).setHeight(500).setWidth(700);
        
        ResourceLink button = new ResourceLink("list.openbutton", new ResourceReference(name) {
            @Override
            public IResource getResource() {
                return new FileArchiveRessource(name, locale);
            }
        });
        
        button.setPopupSettings(popupSettings);
        

        【讨论】:

          猜你喜欢
          • 2013-12-22
          • 1970-01-01
          • 2015-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-04
          • 2023-03-22
          相关资源
          最近更新 更多