从jSF中的ActionListener重定向 [英] Redirect from ActionListener in jSF
问题描述
我使用了JSF和PrimeFaces.我有一个ActionListener,当有人单击MenuItem时会触发它:
Im used JSF and PrimeFaces. I have an ActionListener that fires when someone clicks on a 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);
}
}
在该菜单项中,我使用以下代码将图像数据写入请求流:
In that menu item, I write the data of an image to the request stream using the following code:
/**
* 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之后,我希望图像会显示出来,但是事实并非如此.什么都没发生.我还需要做其他事情才能使图像正确显示吗?
When the ActionListener is done, I expect that the image would be displayed, however, this is not the case. Nothing happens. Is there something else I need to do to get the image to display correctly?
推荐答案
如果请求被ajax触发,则可能会发生这种情况,默认情况下,几乎所有PrimeFaces操作组件都是这种情况.对于JSF/PrimeFaces,应该假设ajax请求返回一个特殊的XML响应,其中包含有关HTML DOM的哪些部分以及如何更新的信息.
That can happen if the request is been fired by ajax, which is by default the case in almost all PrimeFaces action components. In case of JSF/PrimeFaces, ajax requests are supposed to return a special XML response which contains among others information about which parts of the HTML DOM are to be updated and how.
您在这里发送文件下载"作为ajax响应. PrimeFaces的ajax引擎无法解释这种情况.另外,由于安全原因,JavaScript没有强制执行另存为对话框的功能.确实应该由同步请求来请求.
You're here however sending a "file download" as ajax response. This can't be interpreted as such by PrimeFaces' ajax engine. Also, JavaScript has due to security reasons no facilities to force a Save As dialogue. It should really be requested by a synchronous request.
如果是PrimeFaces,则 <p:menuitem>
,则需要添加ajax="false"
属性以关闭ajax性质,并使其触发同步请求.
In case of the PrimeFaces <p:menuitem>
, you'd need to add the ajax="false"
attribute to turn off the ajax nature and let it fire a synchronous request instead.
<p:menuitem ... ajax="false" />
(PrimeFaces的所有其他操作组件,例如<p:commandButton>
,也具有相同的属性)
(same attribute is also available on all other action components of PrimeFaces such as <p:commandButton>
)
什么也没有发生"实际上是不正确的.您已经在网络"网络中看到了具体的HTTP响应. webbrowser内置的webdeveloper工具集的一部分(按Chrome/IE9/Firebug中的F12).您甚至可能在JavaScript控制台中遇到有关无法解释的Ajax响应的JavaScript错误.
That "nothing happens" is actually not true. You'd have seen the concrete HTTP response in the "Network" section of webbrowser's builtin webdeveloper's toolset (press F12 in Chrome/IE9/Firebug). You should probably even have a JavaScript error in the JavaScript console about an uninterpretable ajax response.
这篇关于从jSF中的ActionListener重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!