How to use a4j:mediaOutput correctly for displaying images?

家住魔仙堡 提交于 2019-12-08 02:58:30

问题


Using the code below I can't get the image in the web page. I'm not sure if I understand the documentation in the right way and I'm not able to find any problem with this code.

BEAN

@ManagedBean(name = "imageBean")
@RequestScoped
public class ImageBean {
    public void paint(OutputStream os, Object data) throws IOException {
        BinaryContent content = (BinaryContent) data;
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(content.getContent()));
        ImageIO.write(image, "jpg", os);
    }
}

PAGE

<rich:dataTable value="#{dataProviderBean.aoRequests}" var="item">
    <f:facet name="noData">No messages are available.</f:facet>
    ...
    <rich:column>
        <f:facet name="header">Image data</f:facet>
        <rich:list value="#{item.imageContents}" var="content">
            <a4j:mediaOutput element="img" cacheable="false" session="false"
                createContent="#{imageBean.paint}" value="#{content}" />
        </rich:list>
    </rich:column>
</rich:dataTable>

回答1:


If someone will have the same problem in the future, here is the solution:

The content I put into value attribute is an object which holds binary data of a image. Because it is serialized in URL, the length is too big and it does not work. You have to pass some id and fetch the object in the paint method.

Example

<rich:dataTable value="#{dataProviderBean.aoRequests}" var="item">
    <f:facet name="noData">No messages are available.</f:facet>
    ...
    <rich:column>
        <f:facet name="header">Image data</f:facet>
        <rich:list value="#{item.imageContents}" var="content">
            <a4j:mediaOutput element="img" cacheable="false" session="false"
                createContent="#{imageBean.paint}" value="#{content.id}" />
        </rich:list>
    </rich:column>
</rich:dataTable>

BEAN

public void paint(OutputStream os, Object data) throws IOException {
    String id = (String) data;
    BinaryContent content = (BinaryContent) getContentById(id);
    os.write(content.getContent());
}



回答2:


ImageBean use @SessionScoped or @ApplicationScope https://community.jboss.org/thread/168523



来源:https://stackoverflow.com/questions/7677207/how-to-use-a4jmediaoutput-correctly-for-displaying-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!