gpt4 book ai didi

json - 如何从 Controller 中检索GSP View 中的JSON对象

转载 作者:行者123 更新时间:2023-12-02 14:25:02 24 4
gpt4 key购买 nike

Controller 具有下一个方法(我回收了bootstrap-file-upload plugin的实现):

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":
(...)

在 View 中,存在下一行:
<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 View 以循环遍历它?

最佳答案

要获得这样的变量:

<g:set var="results" value="${g.include(controller: "alojamiento", action: "uploadImage", params: [idAlojamiento:alojamientoInstance.id])}" />  
<g:each in="${JSON.parse(results)}">
<img src="${it.thumbnail_url}"/>
</g:each>

但是,我应该提一下,您应该只在初始 call 中发送此消息。将逻辑放在服务中以使其可重用。

编辑:忘记了它是一个字符串

关于json - 如何从 Controller 中检索GSP View 中的JSON对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com