我用过 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 完成后,我希望图像会显示出来,但事实并非如此。什么都没发生。我还需要做些什么才能使图像正确显示吗?