簡體   English   中英

從jSF中的ActionListener重定向

[英]Redirect from ActionListener in jSF

我使用了JSF和PrimeFaces。 我有一個ActionListener,當有人單擊MenuItem時將觸發它:

@Override
public void processAction(ActionEvent event) throws AbortProcessingException
{
    Resource resouce = getResources().get(0);
    try
    {
        ResourceDelegate delegate = new ResourceDelegate(resouce, configDao);
        JSFUtils.writeToOutputStream(Mime.getMimeTypeForFileType(FileUtilities.getExtension(resouce.getName())), delegate.getData());
    }
    catch(Exception e)
    {
        logger.log(Level.SEVERE, "Cant show resource",e);
    }

}

在該菜單項中,我使用以下代碼將圖像的數據寫入請求流:

/**
 * Writes binary data to the response for the browser to display
 * @param contentType
 * @param data
 * @throws IOException 
 */
public static void writeToOutputStream(String contentType, byte[] data) throws IOException
{
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse hsr = getHttpServletResponse();
    hsr.setContentType(contentType);
    OutputStream stream = hsr.getOutputStream();
    stream.write(data);
    //Tell JSF to skip the remaining phases of the lifecycle
    context.responseComplete();
}

當完成ActionListener時,我希望可以顯示圖像,但是事實並非如此。 什么都沒發生。 我還需要做其他事情才能使圖像正確顯示嗎?

如果該請求被ajax觸發,則可能會發生這種情況,默認情況下,幾乎所有PrimeFaces操作組件都是這種情況。 在JSF / PrimeFaces的情況下,ajax請求應該返回一個特殊的XML響應,其中包含有關HTML DOM的哪些部分以及如何更新的信息。

但是,您在這里發送“文件下載”作為ajax響應。 PrimeFaces的ajax引擎無法解釋這種情況。 另外,由於安全原因,JavaScript沒有強制執行“ 另存為”對話框的功能。 確實應該由同步請求來請求。

如果是PrimeFaces <p:menuitem> ,則需要添加ajax="false"屬性以關閉ajax性質並改為觸發同步請求。

<p:menuitem ... ajax="false" />

(相同的屬性在PrimeFaces的所有其他操作組件上也可用,例如<p:commandButton>

那“什么也沒有發生”實際上是不正確的。 您已經在webbrowser內置的webdeveloper工具集的“網絡”部分中看到了具體的HTTP響應(在Chrome / IE9 / Firebug中按F12)。 您甚至可能在JavaScript控制台中遇到有關無法解釋的Ajax響應的JavaScript錯誤。

也可以看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM