1

The controller has the next method (I'm recycling my implementation of the 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":
            (...)

In the view, there is the next line:

<g:include controller="alojamiento" action="uploadImage" params="[idAlojamiento:alojamientoInstance.id]"/>

So the Internet browser shows a text line with the content of the JSON results variable:

[{"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"},
(...)

I don't want to show that text line. I want to loop through all images. I think it could work:

<g:each in="${results}">
    <img src="${it.thumbnail_url}"/>
</g:each>

How could I to pass the results JSON variable to the GSP view to loop through it?

6
  • The plugin docs says to use <bsfu:fileUpload action="upload" controller="image"/>. Why are you using g:include instead? Commented Aug 19, 2013 at 12:30
  • I'm using <bsfu:fileUpload action="upload" controller="image"/> to upload the images. But my idea is to use the same controller to show the images in a twitter bootstrap carousel. That's why I'm also using g:include in another view. Commented Aug 19, 2013 at 13:24
  • 1
    Why don't you just create another action to display the images? Then you don't need to send them as JSON... Commented Aug 19, 2013 at 13:28
  • Yes, I would repeat some code, but I would be simpler to understand. Probably is the best solution! Commented Aug 19, 2013 at 13:41
  • 1
    To not repeat yourself, you can create a Service and use it in your controllers :-) Commented Aug 19, 2013 at 13:42

1 Answer 1

3

To get a variable like that:

<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>

However I should mention that you should just send this down the initial call. Put the logic in a service to make it reusable.

Edit: Forgot it was a string

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but I don't understand what you mean with "However I should mention that you should just send this down the initial call". Do you mean I can use it just once per view?
It does not work. The error shown is: No such property: thumbnail_url for class: org.codehaus.groovy.grails.web.util.StreamCharBuffer. Performing different tests, I observed that it has the whole results variable. it is: [{"name":"boceto escaleras (...) elete_type":"DELETE"},{"name":"focona la linea de la concepc (...)
Whatever model you pass down initially to the gsp (ie whatever action renders the view), you should include this data
OK. But the loop doesn't work properly. I guess I should refactorize the code (moving more data to a service) to make it reusable both for the bootstrap-file-upload plugin and the code I posted here
I moved the code to a service, but still isn't working. I posted the new question here (I didn't post the service to simplify the question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.