如何在 Grails 中显示图像?
我正在尝试使用以下代码在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事情更简单了。您应该从操作中以映射形式返回模型:
[imageList: imageList]
,以便imageList
在 GSP 中按名称可用。是的,您可以将
images
文件夹移动到web-app
- 全世界都可以通过 HTTP 请求您的图像吗?It was simpler. You should return a model from an action as a Map:
[imageList: imageList]
forimageList
to be available by name in GSP.And yes, can you move
images
folder toweb-app
- is it OK that all the world can request your images via HTTP?您将返回一个 File 对象列表,您将在其中调用文件对象的 toString 方法,该方法很可能返回文件对象的绝对文件路径。
这将在 html 源代码中为您提供类似的内容
尝试调用
如果不起作用,请向我们展示它生成的 html 代码。
新知识,上面的方法行不通。 File.list()的返回实际上是String[],其中每个字符串都是文件名而不是完整路径。
无论如何,查看 html 源代码将有助于了解到底打印出什么内容。
我怀疑 g:each 可能不支持迭代简单的数组类型(如 String[]),您可以尝试将其转换为列表。
您是否尝试过将其转换为列表并使用 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
try calling
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.
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.您无法使用标准图像标签渲染位于 WEB-INF 下的图像。这些图像无法通过网络访问。您将需要另一个控制器来将图像流式传输回视图。像这样:
然后显示此图像:
我将让您将其转换为您自己的需求。然而,关键是您必须将图像流式传输回来,因为它在当前位置无法通过网络访问。
不过,您最好在 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:
And then to display this image:
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.
不要在 WEB-INF 中存储数据,将图像存储在 /web-app/images/
<块引用>
3 你认为:
don't store data in WEB-INF, store your images in /web-app/images/
in your controller:
3 in your view: