In a grails controller I can render an image to the response using
class MyController {
def getImage = {
BufferedImage image = ImageIO.read('http://example.org/foo.png')
ImageIO.write(captcha, "PNG", response.outputStream)
}
}
In a GSP I can render this image using:
<img src="${createLink(controller: 'my', action: 'getImage')}"/>
I would like to write a TagLib that does something similar.
class MyTagLib {
static namespace = "my"
def getImage(attrs ->
BufferedImage image = ImageIO.read('http://example.org/foo.png')
}
}
But I can’t figure out how to render this image to the response. Inside the tag class, I can only write to out which is an instance of GrailsPrintWriter, but ImageIO.write can only write to an OutputStream.
For the sake of completion, the tag above would be invoked from a GSP using
<my:getImage/>
You can make a writer instance of the
GrailsPrintWriter, by doingnew WriterOutputStream(out). Try this:(tested in grails 2.0)