简体   繁体   English

Grails渲染模板还是JSON?

[英]Grails render template or JSON?

I have a controller that uploads and processes a file. 我有一个上传和处理文件的控制器。 Afterwards, I wish to render the processing result in a modal div. 之后,我希望将处理结果呈现为模态div。 I wanted to know what the best way is to get the results from the controller to the modal div on the gsp. 我想知道将控制器的结果传递给gsp上的模态div的最佳方法是什么。 I thought about a template but I didn't know how to specify what the target div for the template should be because this template wouldn't be rendered by a button click where a target for template render is set as an attribute, it would be done on a timed basis (ie when the file is done uploading). 我想到了一个模板,但我不知道如何指定模板的目标div应该是什么,因为这个模板不会通过按钮单击呈现模板渲染的目标被设置为属性,它将是在定时的基础上完成(即文件上传完成时)。 The other way is to send JSON back from the controller but I don't know how to intercept this JSON at the right time because I still don't quite understand the timings of the information flow between the GSP and the Controller. 另一种方法是从控制器发回JSON,但我不知道如何在正确的时间拦截这个JSON,因为我仍然不太了解GSP和Controller之间信息流的时间。 I know how to send the JSON but how to alert the GSP that "hey, some JSON is now ready for your modal that's about to go up." 我知道如何发送JSON,但是如何提醒GSP“嘿,现在有些JSON已准备就绪,你的模态即将上升。” Here is some pseoducode of basically what I am trying to get done. 这是一些基本上我想要完成的pseoducode。

Controller: 控制器:

 upload() {
    // process file and store results in three integers
    // int1 = result1
    // int2 = result2
    // int3 = result3
    // send the three numbers to the gsp
    }

Now what is the best way to get these three numbers to the GSP so that they are displayed on a modal dialog which is about to go up like this: 现在,将这三个数字传递给GSP的最佳方法是什么,以便它们显示在一个模态对话框中,该对话框将会像这样:

<div id="fileUploadResultsModal">
Results:
${int1}, ${int2}, ${int3}
</div>

Here is the JS associated with my ajax upload function: 这是与我的ajax上传功能相关的JS:

$("#chseFile").upload("${createLink(controller: 'customer', action: 'upload',)}",
                                            {dataTypegrp: parseInt(getCheckedValue(document.getElementsByName('dataTypegrp'))),
                                             fileTypegrp: parseInt(getCheckedValue(document.getElementsByName('fileTypegrp')))}, 
                                            function(success) {
                                                    $("#cancel1").trigger("click");
                                                    setTimeout(function(){ 
                                                        $("#summary").trigger("click");
                                                    }, 250);
                                                    displaySuccess(data);
                                            }, 
                                            function(prog, value) {
                                                console.log(value);
                                                $("#prog").val(value);
                                                if (value == 100) {
                                                    $("#prog").hide();
                                                    $("#progressbar").html("Uploading and processing. Please wait...");
                                                }
});

but right now JS complains that 'data' is not defined. 但是现在JS抱怨“数据”没有定义。 'data' is meant to be the JSON coming back from the controller. 'data'是指从控制器返回的JSON。

Thanks 谢谢

you can render them as JSON: 你可以将它们渲染为JSON:

render( [ int1:111, int2:222, int3:333 ] as JSON )

or as a HTML-string 或者作为HTML字符串

render "<div id=\"fileUploadResultsModal\">Results:${int1}, ${int2}, ${int3}</div>"

or use a template 或使用模板

render template:'/yourController/templateName', model:[ int1:111, int2:222, int3:333 ]

or a TagLib TagLib

render g.yourResultTag( int1:111, int2:222, int3:333 )

For this tiny bit of information, the performance is not of concern. 对于这一小部分信息,性能并不值得关注。 It's rather a matter of taste, or what is more appropriate for your client. 这是一个品味问题,或者更适合您的客户。

If the later is JSON-biased, use JSON-rendering. 如果后者是JSON偏向的,请使用JSON渲染。 If it has a mix of JSON and HTML, use others. 如果它混合使用JSON和HTML,请使用其他人。

inside controller at the enf of controller action you can use 您可以使用控制器动作的enf内部控制器

render [data:['name':'firstname','surname':'secondName'] as JSON]

this will render the data to GSP 这将把数据呈现给GSP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM