Wicket动态图片网址 [英] Wicket Dynamic Image URL

查看:171
本文介绍了Wicket动态图片网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短问题:
我需要将一个从数据库拉到一个URL的动态图像,而不用组件添加到显示页面(如使用NonCachingImage)使用Wicket。



我在其他框架中实现的完美解决方案只是创建一个将图像ID作为url参数并将图像呈现给响应流的页面。不幸的是,Wicket的Page类扩展了MarkupContainer,它围绕着MarkupStreams。 MarkupStreams不是非常有助于直接显示字节数据。



长问题:
我正在使用Wicket 1.4.0,在Tomcat 6.0.18中运行。图像存储在通过JDBC检索的Postgres数据库中。图像需要由仅接受图像URL的第三方API呈现。我有一个模型对象,包含字节数据,mime类型和一个Resource对象,可以从DB中提取模型并将其添加到响应流。



任何想法?

解决方案

我刚刚开始与Wicket一起工作,但我只是将资源作为共享资源拥有自己的URL。您只需在应用程序中覆盖 init(),并使用


$ b注册资源$ b

  getSharedResources()。add(resourceKey,dynamicImageResource); 

然后,将其作为共享资源挂载到

  mountSharedResource(path,resourceKey); 

由于某些原因,我还是没有完全把握,你必须先添加类名您传递给的资源密钥的应用程序mountSharedResource()






 创建一个空的Wicket模板mvn archetype:create -DarchetypeGroupId = org.apache.wicket \ 
- DarchetypeArtifactId = wicket-archetype-quickstart \
-DarchetypeVersion = 1.4.0 -DgroupId = com.mycompany \
-DartifactId = myproject

然后,覆盖 WicketApplication 中的 init()通过添加:

  @Override 
protected void init(){
final String resourceKey =DYN_IMG_KEY ;
final String queryParm =id;

getSharedResources()。add(resourceKey,new Resource(){
@Override
public IResourceStream getResourceStream(){
final String query = getParameters()。getString (queryParm);

//生成包含查询参数的图像
final BufferedImage img = new BufferedImage(100,100,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g2 = img.createGraphics();
g2.setColor(Color.WHITE);
g2.drawString(query,img.getWidth()/ 2,img.getHeight()/ 2);

//将图像作为PNG流返回
return new AbstractResourceStreamWriter(){
public String getContentType(){
returnimage / png;

public void write(OutputStream output){
try {ImageIO.write(img,png,output);}
catch(IOException ex){/ *从不吞下异常! * /}
}
};
}
});

mountSharedResource(/ resource,Application.class.getName()+/+
resourceKey);
}

小型动态PNG资源只是在黑色背景上写入查询参数。当然,您可以访问您的数据库,或者做任何您喜欢的图像数据生成。



最后执行 mvn jetty:run ,您将能够访问此网址


Short question: I need to turn a dynamic image pulled from a database into a URL without adding a component to the displaying page (such as using a NonCachingImage) using Wicket.

The perfect solution (that I've implemented in other Frameworks) is simply to create a page that takes the image ID as a url parameter and renders the image to the response stream. Unfortunately Wicket's Page class extends MarkupContainer, which revolves around MarkupStreams. MarkupStreams aren't very conducive to rendering byte data directly.

Long question: I'm using Wicket 1.4.0, running in Tomcat 6.0.18. The image is stored in a Postgres database, retrieved via JDBC. The image needs to be rendered by a third party API that only accepts image URLs. I have a model object that contains the byte data, mime type, and a Resource object that can pull the model from the DB and add it to a response stream.

Any ideas?

解决方案

I've only just started to work with Wicket myself, but I would simply mount the resource as a shared resource with its own URL. You just override init() in your Application and register the resource with

getSharedResources().add(resourceKey, dynamicImageResource);

Then, you mount it as a shared resource with

mountSharedResource(path, resourceKey);

For some reason, that I still do not completely grasp, you have to prepend the class name of the application to the resource key you pass to mountSharedResource().


Let's add a fully working example for some bonus votes! First create an empty Wicket template with

mvn archetype:create -DarchetypeGroupId=org.apache.wicket \
    -DarchetypeArtifactId=wicket-archetype-quickstart \
    -DarchetypeVersion=1.4.0 -DgroupId=com.mycompany \
    -DartifactId=myproject

Then, override the init() method in WicketApplication by adding:

@Override
protected void init() {
    final String resourceKey = "DYN_IMG_KEY";
    final String queryParm = "id";

    getSharedResources().add(resourceKey, new Resource() {
        @Override
        public IResourceStream getResourceStream() {
            final String query = getParameters().getString(queryParm);

            // generate an image containing the query argument
            final BufferedImage img = new BufferedImage(100, 100,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2 = img.createGraphics();
            g2.setColor(Color.WHITE);
            g2.drawString(query, img.getWidth() / 2, img.getHeight() / 2);

            // return the image as a PNG stream
            return new AbstractResourceStreamWriter() {
                public String getContentType() {
                    return "image/png";
                }
                public void write(OutputStream output) {
                    try { ImageIO.write(img, "png", output); }
                    catch (IOException ex) { /* never swallow exceptions! */ }
                }
            };
        }
    });

    mountSharedResource("/resource", Application.class.getName() + "/" +
            resourceKey);
}

The little dynamic PNG resource just writes the query parameter on black background. Of course, you can access your DB or do whatever you like to produce the image data.

Finally, execute mvn jetty:run, and you will be able to access the resource at this URL.

这篇关于Wicket动态图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆