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'll add another answer to say Martin Grigorov wrote a really nice blogpost over at wicketinaction.com to detail how to serve up images loaded from a database in Wicket 1.5:
http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/
This matches exactly with @Michael's question.
Here's my example that does the same for a dynamically compiled list of identifiers, served up as a shared resource with a static URL..
And my ListInitializer that registers the resources as DBNAME_SUBSELECTION1(2/3/..)
I'm sorry but I can't seem to find the tutorial I used to set this up anymore, but it should be evident how this relates to the above example and can be adjusted to do the same for images.. (Sorry for the sparse explanation, if it's still unclear I could come back and edit my answer)
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 yourApplication
and register the resource withThen, you mount it as a shared resource with
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
Then, override the
init()
method inWicketApplication
by adding: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.