在GSP(Grails)中显示图像,从数据库获取链接

3

我是一个Grails新手。我正在尝试为网站中的每个产品显示图像缩略图,就像这样:

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

这里的问题是我想要在数据库中保存图片链接,并通过以下方式获取链接:
${fieldValue(bean: productInstance, field: "image")}  /* 2 */

但是我无法将 /* 2 / 代码替换为 / 1 */ 中的 "Nikon.jpg",这会导致语法错误!
经过一些研究,我发现大多数教程展示了如何显示直接存储在数据库中的图像(例如,在 Grails GSP 中如何显示图像?)。我不确定这种方法是否更好,但我仍然想从数据库中获取图像链接。
我还尝试搜索 Grails 标签库以查找任何支持标签,但没有成功。有人能给我一个提示吗?
4个回答

3
避免语法错误的正确语法应该是: <img src="${resource(dir:'images', file:fieldValue(bean:productInstance, field:'image'))}" /> 但我建议您编写自己的tagLib,因为编写一个非常简单,并且如果您要经常使用此代码,则您的GSP将看起来更漂亮。 您可以轻松编写一个名为<product:image product='productInstance' />的标签,为了增加可用性,您还可以使tagLib输出链接。
在我看来,编写tagLib的简易性确实是Grails最好的功能之一。

谢谢你的回答,现在已经完成了。我打算花更多时间学习tagLib! - Hoàng Long

0

我需要一个安全的解决方案,结合了 http://grails.asia/grails-example-application-simple-document-management-systemhttp://grails.asia/grails-render-images-on-the-fly-in-gsp 。为此,我使用了一个域来存储图像路径,一个gsp来显示图像,以及一个控制器来提供图像。

域:

class Document {
String filename
String fullPath
Date uploadDate = new Date()
static constraints = {
    filename(blank:false,nullable:false)
    fullPath(blank:false,nullable:false)
}

}

Grails 服务器页面:

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main">
        <title>Document List</title>
    </head>
<body>

        <div class="content scaffold-list" role="main">
            <h1>Document List</h1>
        <g:if test="${flash.message}"><div class="message" role="status">${flash.message}</div></g:if>
            <table>
                <thead>
                    <tr>
                    <g:sortableColumn property="filename" title="Filename" />
                    <g:sortableColumn property="uploadDate" title="Upload Date" />
                    <g:sortableColumn property="Preview" title="Vista Previa" />
                </tr>
            </thead>
            <tbody>
                <g:each in="${documentInstanceList}" status="i" var="documentInstance">
                    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                        <td><g:link action="download" id="${documentInstance.id}">${documentInstance.filename}</g:link></td>
                        <td><g:formatDate date="${documentInstance.uploadDate}" /></td>
                        <td><g:img style="height:120px;width:120px;" uri="/doclist/images?id=${documentInstance.id}"  /></td>
                    </tr>
                </g:each>
            </tbody>
        </table>

        <div class="pagination">
            <g:paginate total="${documentInstanceTotal}" />
        </div>
    </div>
</body>
</html>

控制器:

import org.springframework.security.access.annotation.Secured
class DoclistController {

@Secured(['ROLE_ADMIN'])      
def list(){

    params.max = 10
    [documentInstanceList: Document.list(params), documentInstanceTotal: Document.count()]

    //render view: 'list'
}

@Secured(['ROLE_ADMIN'])      
def images(long id){
    Document documentInstance = Document.get(id)
    if ( documentInstance == null) {
        flash.message = "Document not found."
        redirect (action:'list')
    } else {

        def file = new File(documentInstance.fullPath)
        def fileInputStream = new FileInputStream(file)
        def outputStream = response.getOutputStream()
        byte[] buffer = new byte[4096];
        int len;
        while ((len = fileInputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush()
        outputStream.close()
        fileInputStream.close()
    }
}
}

我将以下数据库图像展示如下

DoclistController Result

希望这能有所帮助


0

等一下...如果我字面上理解你的问题,你是在尝试这样做:

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

嗯,那很复杂而且不正确。这个应该可以工作:

<a href="#"><img src="${fieldValue(bean: productInstance, field: "image")}"/></a>

感谢Michael的回复。它有效,但我想从web-app文件夹开始我的路径。资源关键字可以帮助我自动完成这个过程。按照你的方式,我必须使用链接:<myproject>/images/Nikon.jpg。我能否只保存“Nikon.jpg”或“/images/Nikon.jpg”? - Hoàng Long

0

我使用FileUploadService将图片的存储路径storagePath保存在数据库中,例如../../../web-app/personImages/imageName.img扩展名。 用于在GSP中显示图片。

<img style="height:120px;width:102px;"src="${resource(dir:'personImages',file:domainInstance.id + '.png')}" />

示例

首先使用FileUploadSevices文件

领域:

class PersonalDetails {

String avatar

static constraints = {

    avatar(nullable:true,maxSize: 1024000)

}

控制器 save() 操作:

// Save Avatar if uploaded
def avatarImage = request.getFile('avatar')
    if (!avatarImage.isEmpty()) {
    personalDetailsInstance.avatar = fileUploadService.uploadFile(avatarImage, 
       "${personalDetailsInstance.id}.png", "personImages")
        }

数据库文件存储路径:

在头像文件中:

C:\Documents and Settings\Administrator\Documents\workspace-ggts-3.4.0.RELEASE\IDiary\web-app\personImages/1.png

列出GSP:

<img style="height: 120px;width: 102px;"src="${resource(dir:'personImages', file: personalDetailsInstance.id + '.png')}" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接