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

标签 json grails gsp

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/

相关文章:

grails - 我可以在对象而不是类上调用GORM “find”方法吗?

grails - 如何在grails域类中按降序应用索引?

grails - 尝试在GSP中划分变量时出现Grails错误

c++ - Mysql json二进制编码

python - "overload the dot operator"有什么技巧吗?

arrays - 将 JSON 对象列表解码为 perl 中的哈希数组

jsp - grails GSP 中的空间

json - 在 JSON 字符串 Golang 中检测重复项

grails - 将spring-security-ui集成到我的grails应用程序中所需的说明

jquery - Grails注入(inject)g :select and jQuery handler