1

How to get image (@Lob attribute) from database into listcell?

asked 2014-12-16 15:07:16 +0800

demizon gravatar image demizon
169 1 6

Hi guys, i got problem with showing image, which i get from database.

This is how I define picture attribute in model class:

@Lob
@Column (name = "picture")
private byte[] picture;

My question is how can i get this picture and show it in a listcell?

I tried this 2 ways:

1.

<listcell>
 <image content="@load(each.picture)"/>
</listcell>

2.

<listcell>
 <image src="@load(each.picture)"/>
</listcell>

But none of this was working.

Thx for help.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-12-17 07:34:42 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

Zul:

<image content="@load(each.picture) @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;
        }
    }
}

Greetz chill.

link publish delete flag offensive edit

Comments

it works, thank you!!! great job as always chill, thx!

demizon ( 2014-12-17 07:59:39 +0800 )edit

very nice chill! +1

cyiannoulis ( 2014-12-17 08:11:00 +0800 )edit

ready to work. Great +1

terrytornado ( 2014-12-17 08:51:58 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2014-12-16 15:07:16 +0800

Seen: 43 times

Last updated: Dec 17 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More