admin管理员组

文章数量:1291109

I want to pass data from controller to javascript by embedded data directly in view. (So there won't be additional requests.)

My first solution is to use as JSON in GSP like this:

<script>
  var data = ${invoice as JSON};
</script>

I don't think it's good idea since I have to use (Grails 2.2)

grails.views.default.codec = "none"

or (Grails 2.3)

grails {
  views {
    gsp {
      codecs {
        expression = 'none'
      }
    }
  }
}

Now, I found that I can create little taglib like this:

def json = { attrs, body ->
  out << (attrs.model as JSON)
}

And I can use following code in GSP:

<script>
  var data = <g:json model="${invoice}" />;
</script>

Now, the question. Is using taglib is best practice? If not, please give me the best solution.

I want to pass data from controller to javascript by embedded data directly in view. (So there won't be additional requests.)

My first solution is to use as JSON in GSP like this:

<script>
  var data = ${invoice as JSON};
</script>

I don't think it's good idea since I have to use (Grails 2.2)

grails.views.default.codec = "none"

or (Grails 2.3)

grails {
  views {
    gsp {
      codecs {
        expression = 'none'
      }
    }
  }
}

Now, I found that I can create little taglib like this:

def json = { attrs, body ->
  out << (attrs.model as JSON)
}

And I can use following code in GSP:

<script>
  var data = <g:json model="${invoice}" />;
</script>

Now, the question. Is using taglib is best practice? If not, please give me the best solution.

Share Improve this question asked Jan 30, 2014 at 9:36 MeamMeam 2663 silver badges14 bronze badges 4
  • How about sending the json String from the controller to the view, and using ${json.encodeAsJavaScript()}? – user800014 Commented Jan 30, 2014 at 10:13
  • encodeAsJavaScript() gives me: var data = \u007b\u0022key1\u0022:\u0022val1\u0022\u002c\u0022key2\u0022:3.14\u007d – Meam Commented Jan 30, 2014 at 11:05
  • 2 Oh right, in this case you need the raw data. ${raw(json)} – user800014 Commented Jan 30, 2014 at 11:08
  • 2 ${raw(json)} works! However json have to be String, not JSON object. Thanks. – Meam Commented Jan 30, 2014 at 12:06
Add a ment  | 

3 Answers 3

Reset to default 3

Transforming the ment in answer. You can create your JSON String in the controller and pass it to the view. Grails 2.3.x have the raw codec that don't encode your content. More info about this codec here.

Example:

class MyController {
  def index() {
    String invoiceString = invoice as JSON
    [json: invoiceString]
  }
}

index.gsp

<script>
  var data = ${raw(json)};
</script>

using grails 2.4.4.

Above answer did not worked for me.

so adding what worked for me.

Source: http://aruizca./how-to-render-json-properly-without-escaping-quotes-inside-a-gsp-script-tag/

<g:applyCodec encodeAs="none">
    var data = ${data};
</g:applyCodec>

Now, I'm upgrading to Grails 3.2.4. I found that Sérgio Michels' method is still work. Just make sure jsonString is a String object.

<script>
  var data = ${raw(jsonString)};
</script>

If it is not a String object, you can use something like following code.

<script>
  var data = ${raw(mapInstance.encodeAsJSON().toString)};
</script>

本文标签: jsonPassing data from controller to JavaScript in GSPStack Overflow