【问题标题】:Cannot display an image from filesystem in grails无法在 grails 中显示来自文件系统的图像
【发布时间】:2011-11-02 17:33:39
【问题描述】:

我是 grails (1.3.7) 的新手,在显示文件系统中的图像时遇到了奇怪的情况。 one.png 图片被放入 web-app/images/fotos 目录。 zz.gsp:

<img src="${resource(dir:'images/fotos', file:'one.png')}" alt="Nothing" />

与 void 动作相关 def zz = {} 工作正常。但是如果我打算在 rawRenderImage.gsp 中显示同一张图片:

  <body>
   <p>
     ${fdir}  <br/>   ${fname}      <!-- OK -->
   </p>
   <g:if test="${fname}">
      <img src="${resource(dir:'fdir',file: 'fname')}"   alt ="Nothing"/>
   </g:if>  
  </body>

尽管参数 fdir 和 fname 传递给页面,但图片不会出现。控制器中的动作是:

    def rawRenderImage = {
//    def basePath = servletContext.getRealPath("/")
//      def basePath = grailsAttributes.getApplicationContext().getResource("/").getFile().toString() + "/images/fotos/"  
//      def basePath = grailsAttributes.getApplicationContext().getResource("/").getFile().toString()
//      def fname = params.photoId + ".png"
//      def fname = "/images/fotos/" + params.photoId + ".png"

basePath = "images/fotos"        // or basePath=”images” for /images/one.png
fname=”one.png”

        [fdir:basePath, fname:fname]
   }

即使直接分配 basePath=”images/fotos” 和 fname=”one.png” 也不起作用,以及与 basePath 的任何组合 获取绝对路径。即使我将图片放在 images 目录中的情况也不起作用。我使用 netbeans,但它在控制台模式下也不起作用。

请帮忙。

【问题讨论】:

    标签: image grails


    【解决方案1】:

    在模型中将文件名和目录作为变量传递时,不要在标签的src 属性中引用它们。然后 Groovy ${} 评估将评估变量而不是字符串。

    <g:if test="${fname}">
        <img src="${resource(dir:fdir,file:fname)}" alt ="Something"/>
    </g:if>
    

    【讨论】:

    • "${resource(dir:fdir,file: fname)}" -- 结果相同...什么都没有。
    • 这很奇怪。您是否在浏览器中查看了生成的 HTML 以查看 的 src 属性中的 url 是什么?此外,您的示例控制器代码在操作中的basePath 或fname 前面没有def,尽管注释掉的代码有。
    • 你说得对,但是绝对路径我有问题。 Firebug 将服务器的响应提供给 POST rawRenderImage: /home/toly/NetBeansProjects/foto/web-app
      /images/one.png
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 2016-07-10
    • 2020-11-25
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    相关资源
    最近更新 更多