控制器有下一个方法(我正在回收我的bootstrap-file-upload 插件的实现):
def uploadImage() {
String baseName;
String imageExtension = uploadPhotoService.imagesExtension;
String thumbnailExtension = uploadPhotoService.thumbnailsExtension;
switch(request.method){
case "GET":
def results = []
String imagesDirectoryPath = uploadPhotoService.getImageDirectoryDestinationPath(params.idAlojamiento);
def dir = new File(imagesDirectoryPath)
if( dir.exists() ) {
dir.eachFile {
baseName = uploadPhotoService.removeFileExtension(it.getName());
results << [
name: baseName,
size: it.length(),
url: createLink(controller:'alojamiento', action:'picture', params:[imageName: baseName + "." + imageExtension, idAlojamiento: params.idAlojamiento]),
thumbnail_url: createLink(controller:'alojamiento', action:'thumbnail', params:[imageName: baseName + "." + thumbnailExtension, idAlojamiento: params.idAlojamiento]),
delete_url: createLink(controller:'alojamiento', action:'deleteImage', params:[baseName: baseName, idAlojamiento: params.idAlojamiento]),
delete_type: "DELETE"
]
}
}
render results as JSON
break;
case "POST":
(...)
在视图中,有下一行:
<g:include controller="alojamiento" action="uploadImage" params="[idAlojamiento:alojamientoInstance.id]"/>
因此 Internet 浏览器会显示一个带有 JSON 结果变量内容的文本行:
[{"name":"boceto escaleras patio","size":37567,"url":"/AlojamientoPrototipo/alojamiento/picture?imageName=boceto+escaleras+patio.jpg&idAlojamiento=1","thumbnail_url":"/AlojamientoPrototipo/alojamiento/thumbnail?imageName=boceto+escaleras+patio.png&idAlojamiento=1","delete_url":"/AlojamientoPrototipo/alojamiento/deleteImage?baseName=boceto+escaleras+patio&idAlojamiento=1","delete_type":"DELETE"},
(...)
我不想显示该文本行。我想遍历所有图像。我认为它可以工作:
<g:each in="${results}">
<img src="${it.thumbnail_url}"/>
</g:each>
我如何将结果 JSON 变量传递给 GSP 视图以循环遍历它?