-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have the following method that creates an image and returns the path for the zul image tag ... But when I open the application on the browser or in the phone, the image is not displayed.. Can you help me?
ps: in the database the image is a blob entry .
public String getPath() { try{
InputStream in = image.getBinaryStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
File temp = File.createTempFile("tempfile", ".png");
path = temp.getAbsolutePath();
OutputStream outputStream = new FileOutputStream(path);
int bufferSize = 1024;
int length = (int) image.length();
byte[] buffer = new byte[bufferSize];
while((length = in.read(buffer)) != -1)
{
out.write(buffer,0,length);
}
temp.deleteOnExit();
out.writeTo(outputStream);
in.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
//ImageUtils
return path.replace('\\', '/');
}
I have this artigos.zul that contains the listbox with the database data, I use LoadArtigos.java to load the data and I use ControllerArtigos.java to control the events, in this case, when a row from the listbox is selected, ControllerArtigos.java listens to the event and open artigoDetail.zul that contains the detail of the selected item (the image included).
artigos.zul
<zk>
<borderlayout>
<north vflex="min">
<div vflex="1" hflex="1" sclass="topo">
<hbox vflex="1" hflex="1" align="center">
<hbox hflex="1" vflex="1">
<button image="/imagens/return.png" width="50px" height="50px" href="index.zul" />
</hbox>
<hbox hflex="1" vflex="1" align="end" pack="end">
<button image="/imagens/home.png" width="50px" height="50px" href="index.zul" />
</hbox>
</hbox>
</div>
</north>
<center vflex="min">
<div hflex="1" vflex="1">
<listbox vflex="1" hflex="1" id="artigosListbox" apply="artigos.LoadArtigos, artigos.ControllerArtigos" >
<listhead>
<listheader label="Código" />
<listheader label="Descrição" />
</listhead>
<template name="model">
<listitem>
<listcell label="${each.codigo }" />
<listcell label="${each.descricao }" />
</listitem>
</template>
</listbox>
</div>
</center>
<south vflex="min">
<include src="sul.zul" />
</south>
</borderlayout>
</div>
</zk>
LoadArtigos.java
package artigos;
import java.util.List;
import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.select.SelectorComposer; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listbox;
public class LoadArtigos extends SelectorComposer<listbox> {
public void doAfterCompose(Listbox listbox)
{
try {
super.doAfterCompose(listbox);
ArtigosDAO dao = new ArtigosDAO();
List<Artigo> artigos = dao.read();
listbox.setModel(new ListModelList<Artigo>(artigos));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ControllerArtigos.java
package artigos;
import java.util.HashMap; import java.util.Map; import java.util.Set;
import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.select.SelectorComposer; import org.zkoss.zk.ui.select.annotation.Listen; import org.zkoss.zk.ui.select.annotation.Wire; import org.zkoss.zul.*; import org.zkoss.zul.ext.Selectable;
public class ControllerArtigos extends SelectorComposer<component> {
@Wire
private Listbox artigosListbox;
@Wire
private Div global;
@Listen("onSelect = #artigosListbox")
public void showDetail()
{
Set<Artigo> seleccao = ((Selectable<Artigo>) artigosListbox.getModel()).getSelection();
if(seleccao != null && !seleccao.isEmpty())
{
Map data = new HashMap();
Artigo info = seleccao.iterator().next();
data.put("CODIGO", info.getCodigo().toString());
data.put("DESCRICAO", info.getDescricao());
data.put("SRCIMAGEM", info.getCaminho());
data.put("PRECOIVA", info.getPrecoIva().toString());
global.setVisible(false);
Executions.createComponents("artigoDetail.zul", null, data);
}
}
}
artigoDetail
<zk> <borderlayout> <north vflex="min">
<center vflex="min" autoscroll="true">
<vbox hflex="1" vflex="1">
<separator/>
<hbox>
Código:
<textbox value="${arg.CODIGO }" />
</hbox>
<separator/>
<hbox>
Descrição:
<textbox value="${arg.DESCRICAO }" />
</hbox>
<separator/>
<hbox>
Preço:
<textbox value="${arg.PRECOIVA }" />
</hbox>
<separator/>
<hbox>
<image src="${arg.SRCIMAGEM }" />
</hbox>
</vbox>
</center>
<south vflex="min">
<include src="sul.zul" />
</south>
</borderlayout>
</zk>
See if this Upload Images in Zk link will help you bind the image
Hey posh,
Take a look at the AImage Javadoc here.
You can create the image using a File, String (location) or a stream along with a few others. I notice that you seem to already have a stream from the database, you could try and construct the AImage object using that then set the content of the image.
For information on setting the content of the image tag you can follow sjoshi's example and do a search through the docs.
Asked: 2013-06-03 14:55:58 +0800
Seen: 111 times
Last updated: Jun 11 '13
Do not update to ZK Studio 2.0
Catch Server 503 Error - Or Catch Stale Page
how to save spreadsheet data into postgres database?
One controller file with multiple zul file
Why am I getting authentication error for ZK studio activation?
How can i make Tomcat (with virtual hosts) to parse the zul page in subfolders?
What about your ZUl code how you bind the image tag from ZUL page?
sjoshi ( 2013-06-03 18:42:05 +0800 )editI've just edited the topic with your request :)
posh ( 2013-06-03 19:09:45 +0800 )editCan I make cast from blob to AImage when reading from the database ?
posh ( 2013-06-03 19:34:35 +0800 )edit