在zk zul列表框中显示来自DB的图像

时间:2015-02-20 12:52:24

标签: base64 zk

我有一个包含byte []类型的图像的实体。所以返回类型是byte []。我必须得到图像并显示在列表框上。我的代码如下。

public class KitapListesiControl extends SelectorComposer<Component> {

private static final long serialVersionUID = 1L;

private List<Kitaplar> kitapList = new ArrayList<Kitaplar>();
private ListModel<Kitaplar> kitapListModel;

@Wire
private Listbox kitapListBox;

@Wire
private Image kitapImageId = new Image();

KitapIslemleriDao kid = new KitapIslemleriDaoImpl();

@Override
public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);
    kitapList = kid.findAllKitaplar();
    kitapListModel = new ListModelList<Kitaplar>(kitapList);
    kitapListBox.setModel(kitapListModel);
    kitapImageId.setId("kitapImageId" + kitapList.get(0).getKitapId());
}

我想在单元格上显示图像的列表框位于

之下
<listbox id="kitapListBox" emptyMessage="Kayıt yok!"
                mold="paging" pageSize="5" width="970px" checkmark="true"
                apply="com.mesutemre.kitapIslemleri.KitapListesiControl"
                model="${win$composer.kitapListModel}">

                <listhead>
                    <listheader width="32px" />
                    <listheader label="Kitap Adı" align="center"
                        sort="auto(kitapad)" width="190px" />
                    <listheader label="Yazar Adı" align="center"
                        sort="auto(yazarad)" width="190px" />
                    <listheader label="Kitap Tür" align="center"
                        width="190px" sort="auto(kitaptur)" />
                    <listheader label="Kitap Durum" align="center"
                        sort="auto(kitapdurum)" width="190px" />
                    <listheader label="Kitap Resmi" align="center"
                        width="190px" />
                </listhead>
                <template name="model">
                    <listitem>
                        <listcell />
                        <listcell label="${each.kitapad}" />
                        <listcell label="${each.yazarad}" />
                        <listcell label="${each.kitaptur}" />
                        <listcell label="${each.kitapdurum}" />
                        <listcell>
                            <image
                                src="@load(each.kitapimage) @converter('com.mesutemre.converter.ImageToZkImageConverter')" />
                        </listcell>
                    </listitem>
                </template>
            </listbox>

1 个答案:

答案 0 :(得分:1)

祖尔:

<image content="@load(each.image) @converter('be.chillworld.web.vm.util.ImageToZkImageConverter')" />

ImageToZkImageConverter.class:

package be.chillworld.web.vm.util;

import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.zkoss.bind.BindContext;
import org.zkoss.bind.Converter;
import org.zkoss.image.AImage;
import org.zkoss.zul.Image;

public class ImageToZkImageConverter implements Converter<AImage, byte[], Image> {

    private Log logger = LogFactory.getLog(ImageToZkImageConverter.class);

    @Override
    public byte[] coerceToBean(AImage compAttr, Image component, BindContext ctx) {
        logger.debug("Converting the image");
        return compAttr.getByteData();
    }

    @Override
    public AImage coerceToUi(byte[] beanProp, Image component, BindContext ctx) {
        try {
            if (beanProp != null && beanProp.length > 0) {
                AImage im = new AImage("", beanProp);
                component.setContent(im);
                return im;
            }
            logger.debug("Return null => image is empty");
            return null;
        } catch (IOException e) {
            logger.error("Error occured, returning null", e);
            return null;
        }
    }
}

来源:http://forum.zkoss.org/question/95645/how-to-get-image-lob-attribute-from-database-into-listcell/#95651

对于 MVC ,您可以执行以下操作:

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
....

    <image content="${c:new2('org.zkoss.image.AImage','',each.image)}" />
 ....