如何在 Grails 中显示图像?

发布于 2024-11-03 08:34:16 字数 1144 浏览 3 评论 0原文

我正在尝试使用以下代码在 GSP 页面中渲染 /WEB-INF/images/sps 中的图像:

def index = {
    def baseFolder = grailsAttributes.getApplicationContext().getResource("/").getFile().toString()
    println baseFolder
    def imagesFolder = baseFolder + '/images/sps'
    println imagesFolder
    def imageList1 = new File(imagesFolder).list()
    println imageList1
    def imageList = Arrays.asList(imageList1)       
    println imageList
    imageList
    //redirect(action:"displayImages", params:["imageList":imageList])
    //render(view:"displayImages")
}

控制器能够从文件系统读取图像。但是当我尝试使用以下代码显示它们时,图像没有出现。

index.gsp

<g:each in="${imageList}" var="image">
   <img src="${resource(dir: 'images', file: image.filename)}" alt="Grails"/>
</g:each>

我犯了什么错误?

编辑

当我尝试使用单个图像时,它工作正常 - 我能够查看 WEB-INF/images 文件夹中的图像

<img src="${resource(dir: 'images', file: '1.jpg')}" alt="Grails"/>

并且没有 HTML为循环代码生成的代码(在index.gsp 代码上方)。它只是空白。

我的要求是显示文件系统文件夹中的所有图像文件。

I am trying to render the images from /WEB-INF/images/sps in the GSP page using the following code:

def index = {
    def baseFolder = grailsAttributes.getApplicationContext().getResource("/").getFile().toString()
    println baseFolder
    def imagesFolder = baseFolder + '/images/sps'
    println imagesFolder
    def imageList1 = new File(imagesFolder).list()
    println imageList1
    def imageList = Arrays.asList(imageList1)       
    println imageList
    imageList
    //redirect(action:"displayImages", params:["imageList":imageList])
    //render(view:"displayImages")
}

The controller is able to read the images from the fileSystem. But when I try to display them using the following code, the images are not coming.

index.gsp

<g:each in="${imageList}" var="image">
   <img src="${resource(dir: 'images', file: image.filename)}" alt="Grails"/>
</g:each>

What mistake am I doing?

EDIT:

When I try with single image, it is working fine - I am able to view the image that is in the WEB-INF/images folder

<img src="${resource(dir: 'images', file: '1.jpg')}" alt="Grails"/>

And there is no HTML code thats getting generated for the loop code(above index.gsp code). Its just blank.

My requirement is to display all the image files that are on the file system folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海 2024-11-10 08:34:16

事情更简单了。您应该从操作中以映射形式返回模型:[imageList: imageList],以便 imageList 在 GSP 中按名称可用。

是的,您可以将 images 文件夹移动到 web-app - 全世界都可以通过 HTTP 请求您的图像吗?

It was simpler. You should return a model from an action as a Map: [imageList: imageList] for imageList to be available by name in GSP.

And yes, can you move images folder to web-app - is it OK that all the world can request your images via HTTP?

霊感 2024-11-10 08:34:16

您将返回一个 File 对象列表,您将在其中调用文件对象的 toString 方法,该方法很可能返回文件对象的绝对文件路径。
这将在 html 源代码中为您提供类似的内容

<img src="/app/images/c:\path\to\imagefile.png">

尝试调用

<img src="${resource(dir: 'images', file: image.name)}" alt="Grails"/>

如果不起作用,请向我们展示它生成的 html 代码。


新知识,上面的方法行不通。 File.list()的返回实际上是String[],其中每个字符串都是文件名而不是完整路径。

无论如何,查看 html 源代码将有助于了解到底打印出什么内容。

我怀疑 g:each 可能不支持迭代简单的数组类型(如 String[]),您可以尝试将其转换为列表。

def imageList = Arrays.asList(new File(imagesFolder).list())

您是否尝试过将其转换为列表并使用 g:each ?

为什么要将图像存储在 WEB-INF/images 中?为什么不只是图像?我认为代码 ${resource(dir:'images')} 会指向后者。

you are returning a list of File objects, where you will call the toString method of the file object which most likely returns the absoute file path of the file object.
this would give you something like this in the html source code

<img src="/app/images/c:\path\to\imagefile.png">

try calling

<img src="${resource(dir: 'images', file: image.name)}" alt="Grails"/>

and if that doesnt work, show us the html code that it produces.


In light of new knowledge, the above won't work. The return of File.list() is actually String[] where each string is a file name rather than a complete path.

Anyways, getting a look at the html source would shed light on what exactly gets printed out.

I suspect that maybe g:each doesn't support iterating over simple array types like String[], you could try converting it to a List.

def imageList = Arrays.asList(new File(imagesFolder).list())

Have you tried converting it to a list and using g:each with that?

why are you storing your images in WEB-INF/images? why not just images? i think the code ${resource(dir:'images')} would point to the latter.

铁轨上的流浪者 2024-11-10 08:34:16

您无法使用标准图像标签渲染位于 WEB-INF 下的图像。这些图像无法通过网络访问。您将需要另一个控制器来将图像流式传输回视图。像这样:

class AvatarController {

  def show = {

    def userInstance = User.get(params.id)
    def avatarFilePath = new File(userInstance.avatarURL)
    response.setContentType("application/png")
    response.setContentLength(avatarFilePath.size().toInteger())
    OutputStream out = response.getOutputStream();
    out.write(avatarFilePath.bytes);
    out.close();
  }
}

然后显示此图像:

<img src="/app/avatar/1234" />

我将让您将其转换为您自己的需求。然而,关键是您必须将图像流式传输回来,因为它在当前位置无法通过网络访问。

不过,您最好在 WEB-INF 之外为它们提供服务。

You can't render images that are located under WEB-INF using the standard image tag. The images aren't web accessible. You'll need another controller that will stream the images back to the view for you. So something like this:

class AvatarController {

  def show = {

    def userInstance = User.get(params.id)
    def avatarFilePath = new File(userInstance.avatarURL)
    response.setContentType("application/png")
    response.setContentLength(avatarFilePath.size().toInteger())
    OutputStream out = response.getOutputStream();
    out.write(avatarFilePath.bytes);
    out.close();
  }
}

And then to display this image:

<img src="/app/avatar/1234" />

I'll let you work out the conversion of this into your own needs. The key, however, is that you must stream the image back since it isn't web accessible in its current location.

You're better off just serving them outside of WEB-INF, however.

挽容 2024-11-10 08:34:16
  1. 不要在 WEB-INF 中存储数据,将图像存储在 /web-app/images/

  2. < p>在你的控制器中:

    <块引用>

    def baseFolder = servletContext.getRealPath("/")
    def 文件夹 = baseFolder + '/images/' // web-app/images/
    def imagesFolder = 新文件(文件夹)
    def 文件 = imagesFolder.listFiles().toList()
    列表<字符串>图片列表 = []
    文件.each {
        imageList.add(作为字符串)
    }
    返回图像列表
    

3 你认为:

   <g:each in="${imageList}" var="image">
       <img src="${resource(dir: 'images', file: image)}" alt="Grails"/>
    </g:each>
  1. don't store data in WEB-INF, store your images in /web-app/images/

  2. in your controller:

    def baseFolder = servletContext.getRealPath("/")
    def folder = baseFolder + '/images/' // web-app/images/
    def imagesFolder = new File(folder)
    def files = imagesFolder.listFiles().toList()
    List<String> imageList = []
    files.each {
        imageList.add(it as String)
    }
    return imageList
    

3 in your view:

   <g:each in="${imageList}" var="image">
       <img src="${resource(dir: 'images', file: image)}" alt="Grails"/>
    </g:each>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文