如何在view.gsp中访问JSON对象的元素以用作图像调用中的参数?

时间:2014-06-09 17:44:39

标签: json groovy portlet

我想我正在将一个JSON对象从我的控制器传递给我的视图,但我似乎无法访问JSON的谨慎元素。请分享您的想法。

  • 我应该需要多少DisplayRecentPortlet 访问那里的JSON元素?
  • 我真的通过JSON吗?
  • 我如何访问jsonObj中的各个元素来构造一个 视图中的img标签?

DisplayRecentContoller.groovy

package cssinstaviewer

import groovy.json.*
import grails.converters.JSON

class DisplayRecentController {

    def view() { 

       def apiUrl = new URL("https://api.instagram.com/v1/users/STUFFTHATWORKS/media/recent/?client_id=STUFFTHATWORKS")
       def doc = new JsonSlurper().parseText(apiUrl.text)
       def jsonObj = new JsonBuilder(doc.data.images.low_resolution[0])

                        render(view:"view", model: [jsonObj: jsonObj as JSON])  

    }
}

DisplayRecentPortlet.groovy

package cssinstaviewer

import javax.portlet.*

class DisplayRecentPortlet {

    def title = 'CSS Instagram Viewer'
    def description = '''
CSS Instagram Viewer
'''
    def displayName = 'CSS Instagram Viewer'
    def supports = ['text/html':['view']]

    //uncomment to declare events support
    //def events = [publish: ["event-1"], process: ["event-2"]]

    //uncomment to declare public render parameter support
    //def public_render_params = ["prp-1","prp-2"]

    // Used for liferay
    // @see http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/anatomy-of-a-portlet
     def liferay_display_category = "category.St. Scholastica"

    def actionView = {
        //TODO Define action phase for 'view' portlet mode
        portletResponse.setRenderParameter("prp-1", "value-1");
    }

    def eventView = {
        //TODO Define event phase for 'view' portlet mode.
        def eventValue = portletRequest.event.value
    }

    def renderView = {
        //TODO Define render phase for 'view' portlet mode.
        //Return the map of the variables bound to the view,
        //in this case view.gsp if it exists or render.gsp if not
        ['jsonObj':'jsonObj']
    }

    def resourceView = {
        //TODO define resource phase for 'view' portlet mode.
        //Render HTML as response
        render {
            html {
                head()
                body {
                    "Render me"
                }
            }
        }
    }

    def actionEdit = {
        //TODO Define action phase for 'edit' portlet mode
        portletResponse.setEvent("event-1","event-1")
        portletResponse.setPortletMode(PortletMode.VIEW)
    }

    def renderHelp = {
        //TODO Define render phase for 'help' portlet mode
        //Return the map of the variables bound to the view,
        //in this case help.gsp if it exists or render.gsp if not
        ['mykey':'myvalue']
    }

    def doResource = {
        //TODO Define handling for default resource URL handling method, independent of porlet mode
        //Return the map of the variables bound to the view,
        //in this case resource.gsp
        ['mykey':'myvalue']
    }

    //invoked by setting 'action' param in resourceURL (as an example) to 'doSomethingAjaxy'
    def doSomethingAjaxy =  {
        //render JSON
        render(contentType:"text/json") {
            example(mykey:"myvalue")
        }
    }

    //invoked by setting 'action' param in eventURL (as an example) to 'handleThisEvent'
    def handleThisEvent =  {
        //render thisEvent.gsp
        render(view:"thisEvent")
    }

}

view.gsp

<%@page import="cssinstaviewer.DisplayRecentPortlet"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<div>
 <g:set var="instaImg" value="${jsonObj}" />
 instImg = ${instaImg}
 <hr />
jsonObj ${jsonObj}"
</div>

输出

instImg = {"content":{"height":306,"width":306,"url":"http://scontent-a.cdninstagram.com/hphotos-xap1/t51.2885-15/925254_1484378108464185_1325554272_a.jpg"}} jsonObj {"content":{"height":306,"width":306,"url":"http://scontent-a.cdninstagram.com/hphotos-xap1/t51.2885-15/925254_1484378108464185_1325554272_a.jpg"}}"

2 个答案:

答案 0 :(得分:0)

好吧,我不需要从我改变的控制器发送JSON

render(view:"view", model: [jsonObj: jsonObj as JSON])  

render(view:"view", model: [jsonObj: jsonObj])

然后我可以通过视图中的键名分别处理元素。

<img src="${instaImg.content.url}" width="${instaImg.content.width}" height="${instaImg.content.height}">

答案 1 :(得分:0)

将JSON对象放在模型中就像你正在做的那样有点奇怪但除此之外,我会尝试解决你的具体问题......

在您的控制器中,您可以执行以下操作,将图像路径添加到模型中:

def view() { 
    def apiUrl = new URL("https://api.instagram.com/v1/users/STUFFTHATWORKS/media/recent/?client_id=STUFFTHATWORKS")
    def doc = new JsonSlurper().parseText(apiUrl.text)
    def jsonObj = new JsonBuilder(doc.data.images.low_resolution[0])
    def imagePath = jsonObj.content.url
    render(view:"view", model: [jsonObj: jsonObj as JSON, imagePath: imagePath])  
}

然后在你看来你可以做这样的事情......

<img src="${imagePath}"/>

你也可以这样做......

<img src="${jsonObj.content.url}"/>

您正在寻找的那种东西是什么?

您还应该查看http://grails.org/doc/latest/ref/Tags/img.html作为生成img标记的机制。