在gsp中显示创建的图像

在gsp中显示创建的图像

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

问题描述

我对Grails非常陌生,因此对这个问题可能有一个非常简单的答案。我试图在gsp中显示一个动态创建的图像。图像不存储在数据库中,它在控制器中实时创建。

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

所以基本上在requestGraph.gsp中我有一个类似于以下内容的代码片段:

 < g:form action =displayGraph> 
< g:submitButton name =displayGraphvalue =显示图形/>
< / g:表格>

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

  def requestGraph = {} 

def displayGraph = {
// ...使用params生成图片字节数组并赋值给var img
return [image:img]
}

在displayGraph.gsp中:

 < body> 
< h1>图表标题< / h1>
< / body>

我试着将图像直接传递给displayGraph动作中的输出流。这是有效的,但后来我失去了对displayGraph.gsp中所有页面格式的控制。

我发现的大多数教程都会创建一个专门的操作来将图像传输到输出流,然后使用标记调用该操作。问题是我的图像没有存储在数据库中,我看不到传递图像字节数组(甚至是表单参数)来创建/渲染图像的方法。任何人都可以帮助我吗?谢谢。

解决方案

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

  //控制器动作
def displayGraph = {
def img / / byte数组
// ...
response.setHeader('Content-length',img.length)
response.contentType ='image / png'//或适当的图像内容键入
response.outputStream<< img
response.outputStream.flush()
}

然后,您可以访问您的图片位于< img> 标记的 src 中,如下所示:

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

更新

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


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.

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.

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]
}

Within displayGraph.gsp:

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

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.

解决方案

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()
}

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

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

Update:

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中显示创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 19:30