控制器有下一個方法(我回收我的執行bootstrap-file-upload plugin的):如何從控制器獲取的GSP視圖JSON對象
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]"/>
因此,互聯網瀏覽器顯示了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視圖來循環它?
的插件文檔說,使用' '。你爲什麼要用'g:include'來代替? –
我正在使用' '來上傳圖片。但我的想法是使用相同的控制器來顯示twitter引導輪播中的圖像。這就是爲什麼我在另一個視圖中也使用'g:include'的原因。 –
chelder
爲什麼不只是創建另一個動作來顯示圖像?那麼你不需要把它們作爲JSON發送...... –