例子参考:
T3自带的workbeanch里有
1.ChartService class
2.ChartAsset class
3.IChartProvider的例子代码
思路:
因为在DAO,service里返回的Info对象里已经有图片的属性,
类型是byte[]
所以直接out.write(byte[])获得图片.
题外:
其中,AbstractService相比T4少了Webresponse属性,
不可以直接在IChartProvider里定义
public void sendDynaAsset(WebResponse response);
然后在实现类里用这句
OutputStream out = response.getOutputStream(new ContentType("image/jpeg"));
来操作out.write .
T3里都在AbstractService里操作out, service()方法里就有参数ResponseOutputStream output
代码:
1. ChartService
public class ChartService extends AbstractService{
public void service(
IEngineServiceView engine,
IRequestCycle cycle,
ResponseOutputStream output)
throws ServletException, IOException
{
.
try
{
IChartProvider provider = (IChartProvider) component;
byte[] bytes = provider.getImageBytes();
//默认规则,page页面里若数据库里没有bytes,则返回null
if (bytes==null)
//显示默认图片
bytes = drawDefaultImage();
output.setContentType("image/jpeg");
// I've seen a few bits of wierdness (including a JVM crash) inside this code.
// Hopefully, its a multi-threading problem that can be resolved
// by synchronizing.
synchronized (this)
{
output.write(bytes);
}
}
}
protected byte[] drawDefaultImage() {
int width = 102, height = 126;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.RED);
g.setFont(new Font("Default", Font.PLAIN, 22));
g.drawString("暂无照片", 10, height / 2 - 10);
byte buffer[] = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", out);
buffer = out.toByteArray();
} catch (Exception e) {
log.info("绘制默认图片失败");
}
return buffer;
}
}
2. ChartAsset
与workbeanch里的CharAsset一样,没有任何增删
public class ChartAsset extends AbstractAsset{
private IEngineService _chartService;
private IComponent _chartProvider;
public ChartAsset(IRequestCycle cycle, IComponent chartProvider)
{
super(null, null);
IEngine engine = cycle.getEngine();
_chartService = engine.getService(ChartService.SERVICE_NAME);
_chartProvider = chartProvider;
}
}
3. IChartProvider
Service类回调接口
public interface IChartProvider
{
public byte[] getImageBytes();
}
4.Page类
需要实现IChartProvider回调接口的getImageBytes()方法
public abstract SaryPage extends BasePage implements IChartProvider{
private IAsset chartImageAsset;
public IAsset getChartImageAsset() {
if (chartImageAsset == null)
chartImageAsset = new ChartAsset(getRequestCycle(), this);
return chartImageAsset;
}
/** *//**
* This method is invoked by the service (in a seperate request cycle from
* all the form handling stuff). The {@link #getChartImageAsset()} method
* provides an {@link IAsset} that is handled by the {@link ChartService},
* and the asset encodes the identity of this page.
*
*/
public byte[] getImageBytes() {
if (getSary().getXp()!=null)
return getSary().getXp();
else
//默认规则,page页面里若数据库里没有bytes,则返回null
return null;
}
}
5. Html页面
<img jwcid="@Image" width="102" height="126" image="ognl:chartImageAsset"/>
6. .application文件 注册ChartService
<application name="SaryProject" engine-class="cn.vikings.tapestry.common.SaryEngine">
<service name="chart" class="cn.vikings.tapestry.common.chart.ChartService"/>
</application>
图片显示搞店. 就这么简单.
不用再很BT得传一句HQL语句和图片ID来,再用JDBC查一次数据库.