在 GSP (Grails) 中显示图像,从数据库获取链接 [英] Displaying image in GSP (Grails), get link from database

查看:14
本文介绍了在 GSP (Grails) 中显示图像,从数据库获取链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Grails 新手.我正在尝试为网站中的每个产品显示图像缩略图,如下所示:

I'm a Grails newbie. I'm trying to show an images thumbnail for every product in a site, like this:

<a href="#"><img src="${resource(dir:"images", file: "Nikon.jpg") }"/></a>/* 1 */

这里的问题是我想将图片链接保存在数据库中,并通过以下方式获取链接:

The problem here is that I want to save the image link in the database, and get the link by:

${fieldValue(bean: productInstance, field: "image")}  /* 2 */

但是我无法将/* 2 /代码替换到/ 1 */中Nikon.jpg"的位置,导致语法错误!

But I can't replace the /* 2 / code into the places of "Nikon.jpg" in / 1 */, it causes syntax error!

经过一番研究,我发现大多数教程都展示了如何显示直接存储在数据库中的图像(例如,如何在 grails GSP 中显示图像?).我不确定这种方法是否更好,但我仍然想从数据库中获取图像链接.

After doing some research, I see that most tutorials show how to display image that stores directly in database (for example, How to display image in grails GSP?). I'm not sure if that approach is better, but I still want to take image link from database.

我也尝试搜索 grails 标签库以找到任何支持标签,但没有成功.谁能给个提示?

I also tried to search grails tag library to find any support tag, but with no success. Can anyone give me a hint?

推荐答案

避免语法错误的正确语法是:

The proper syntax to avoid a syntax errors would be:

<img src="${resource(dir:'images', file:fieldValue(bean:productInstance, field:'image'))}"/>

但我建议您编写自己的 tagLib,因为编写一个确实非常简单,如果您打算大量使用此代码,您的 GSP 会看起来更好.您可以轻松编写一个标签,其名称如下:<product:image product='productInstance'/> 并且为了额外的可用性,您可以让 tagLib 也输出链接.

But I recommend you write your own tagLib, because it is really very simple to write one and your GSPs will look much nicer if you are going to use this code a lot. You could easily write a tag that would be called something like: <product:image product='productInstance' /> and for extra usability you could make the tagLib output the link as well.

在我看来,编写 tagLibs 的简单性确实是最好的 grails 特性之一.

The simplicity of writing tagLibs is really one of the best grails features in my opinion.

这篇关于在 GSP (Grails) 中显示图像,从数据库获取链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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