Grails:在 gsp 中显示创建的图像 [英] Grails: displaying created image in gsp

查看:20
本文介绍了Grails:在 gsp 中显示创建的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Grails 很陌生,所以这个问题可能有一个非常简单的答案.我正在尝试在 gsp 中显示动态创建的图像.图像不存储在数据库中,它是在控制器中动态创建的.

I'm very new to Grails so there's probably a very simple answer to this question. I'm trying to display a dynamically created image in a gsp. The image is NOT stored in a database, it's created on the fly in the controller.

我基本上拥有的是一个 gsp,它有一个接受一组用户输入的表单 (requestGraph.gsp).提交表单后,参数被发送到控制器中的 displayGraph 操作,该操作从完全在 Grails 之外的数据库中查询信息,并使用 JFreeChart 库创建图表.我想在 displayGraph.gsp 或类似内容中显示此图像.

What I essentially have is one gsp that has a form which takes in a set of user inputs (requestGraph.gsp). Upon submitting the form, the parameters are sent to the a displayGraph action in the controller which queries information from a database completely outside of Grails and creates a chart using the JFreeChart library. I would like to display this image within a displayGraph.gsp or something like that.

所以基本上在 requestGraph.gsp 中我有一个类似于:

So basically within requestGraph.gsp I have a snippet similar to:

<g:form action="displayGraph">
    <!-- ... bunch of labels and boxes -->
    <g:submitButton name="displayGraph" value="Display Graph" />
</g:form>

在控制器中我有类似的东西:

Within the controller I have something like:

def requestGraph = {}

def displayGraph = {
    //... code that uses params  to make an image byte array and assigns to var img
    return [image : img]
}

在 displayGraph.gsp 中:

Within displayGraph.gsp:

<body>
    <h1>Graph Title</h1>
    <!-- ??? How to dislpay image? -->
</body>

我尝试将图像直接传送到 displayGraph 操作中的输出流.这有效,但随后我失去了对 displayGraph.gsp 中所有页面格式的控制.

I tried piping the image directly to the output stream in the displayGraph action. This works, but then I lose control of all page formatting in displayGraph.gsp.

我发现的大多数教程都创建了一个专用操作,将图像通过管道传输到输出流,然后使用标签调用该操作.问题是我的图像没有存储在数据库中,我看不到通过图像字节数组(甚至表单参数)来创建/渲染图像的方法.有人可以帮我解决这个问题吗?谢谢.

Most tutorials I've found create a dedicated action to pipe the image to an output steam then call that action using a tag. The problem is that my image isn't stored in a database and I see no way of passing the image byte array (or even the form parameters) to create/render the image. Can anybody help me with this? Thanks.

推荐答案

如果将字节写入输出流,则可以将控制器/操作视为 GSP 中图像的来源.这是一个未经测试的快速示例:

If you write the bytes to the output stream, you can treat the controller/action as the source of the image in your GSP. Here's a quick, untested example:

// controller action
def displayGraph = {
    def img // byte array
    //...
    response.setHeader('Content-length', img.length)
    response.contentType = 'image/png' // or the appropriate image content type
    response.outputStream << img
    response.outputStream.flush()
}

然后您可以在 <img> 标签的 src 中访问您的图像,如下所示:

You could then access your image in the src of an <img> tag like this:

<img src="${createLink(controller: 'myController', action: 'displayGraph')}"/>

更新:

再次阅读您的问题后,这可能有效,也可能无效 - 看起来您可能正在显示作为表单提交结果的图表.这仅在您将状态存储在服务器上的某处时才有效(而不是仅从提交表单的一个请求中获取它).如果您确实在服务器上存储了足够的状态来生成图形,那么您必须向控制器提供一些额外的参数以获得正确的图像,例如src="${g.link(controller: 'myController', action: 'displayGraph', params: ['id': 1234])}",其中 id 是您检索图形状态的方式.

After reading your question again, this may or may not work - it looks like you might be displaying the graph as the result of a form submission. This will only work if you're storing the state on the server somewhere (instead of just getting it from the one request where the form is submitted). If you do store enough state on the server to generate the graph, then you'd have to provide some additional parameters to your controller to get the correct image, e.g. src="${g.link(controller: 'myController', action: 'displayGraph', params: ['id': 1234])}", where id is how you retrieve the graph state.

这篇关于Grails:在 gsp 中显示创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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