2011-11-02 71 views
0

我是grails(1.3.7)的新手,我一直处于一种奇怪的情况,显示来自文件系统的图像。将one.png图片放入web-app/images/fotos目录。该zz.gsp无法在grails中显示来自文件系统的图像

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

相关的无效动作高清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> 

图片不显示的参数FDIRFNAME通页面inspite。在控制器中的操作是:

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

即使直接受让人基本路径=”图像/照片”FNAME =” one.png”不工作,以及与任意组合基本路径获得绝对路径。甚至当我把图片放在图片目录不起作用的情况下。我使用netbeans,但它也不能在控制台模式下工作。

请帮忙。

回答

2

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

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

“$ {resource(dir:fdir,file:fname)}” - 相同的结果...没有。 – tolymark

+0

这很奇怪。您是否在浏览器中查看了生成的HTML,以查看的src'属性中的URL?另外,你的示例控制器代码在动作中没有'basePath'或'fname'前面的'def',虽然注释掉了。 – schmolly159

+0

你是对的报价,但绝对路径我有问题。 萤火虫给出了从服务器到POST rawRenderImage响应: /家庭/ TOLY /的NetBeansProjects /照片/ Web的应用程序
/images/one.png Nothing 并获得one.png地址是http://本地主机:8080/foto/home/toly/NetBeansProjects/foto/web-app/images/one.png 产生404 Not Found错误...但image one.png位于images文件夹内,并显示相对路径。 – tolymark

相关问题