简体   繁体   中英

GRAILS: How is .gsp file parsed?

I found a code snippet that looks like this

createReportProfile = function () {

var url = "<%= createLink(controller: 'configCashFlowReport', action: 'saveCreate')%>";
var data = $("form[id=addReportConfigForm]").serializeArray();
var doneFunction = function (data) {
    $("#returnAddReportProfiletDiv").html(data);
}

ajaxGet(url, data, doneFunction, undefined);

return false; }

Please take note of this line

var url = "<%= createLink(controller: 'configCashFlowReport', action: 'saveCreate')%>";

CreateReportProfile function is inside script tags and is saved in report.gsp. Obviously, the code is written using javascript but there is a grails code inside it (createLink).

1) How was grails able to detect that it should evaluate the createLInk tag? Because of <%= %> ?? But it is inside a double quote. How does grails differentiate a simple string and a string that should be evaluated?

2) How is .gsp file parsed?

Why you think such code is something hard to parse? For GSP it doesn't matter what content it have, html, js, or css, or anything else. All of this is text. There are no semantics associated with it, besides GSP tags/blocks itself. Everything else outside such blocks are characters passed as is.

You can use <g:createLink /> syntax instead, or ${createLink} . I think <%= createLink %> was used there just because it's easier to read in Javascript code, and most IDEs will highlight it correctly w/o any extra effort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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