codeconsole commented on code in PR #16139: URL: https://github.com/apache/grails-core/pull/16139#discussion_r3780767578
########## grails-doc/src/en/guide/theWebLayer/gsp/gspStaticCompilation.adoc: ########## @@ -0,0 +1,216 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +//// + +GSP pages are compiled dynamically by default. A page can instead be statically compiled, so that expressions and scriptlets in the page are type checked at compile time and dispatched without dynamic lookup at render time. + +Static compilation is enabled per page with the `compileStatic` page directive: + +[,xml] +---- +<%@ page compileStatic="true" %> +---- + +==== Declaring the Model + +A page can state what it is rendered with, using the `model` page directive, which names and types each variable: + +[,xml] +---- +<%@ page model="Book book" %> +<h1>${book.title}</h1> +---- + +Separate multiple declarations with semicolons, or use a multi-line value: + +[,xml] +---- +<%@ page model="Book book; List<Review> reviews" %> +---- + +Because a declared model is what makes a page type checkable, the `model` directive enables static compilation on its own — `compileStatic="true"` is implied and does not need to be given as well. + +Declaring a model also states that the model is complete, so reading a name outside it is reported rather than left to the render: + +---- +The variable [autor] is undeclared. Review Comment: Any name outside the model is reported whether it's spelled correctly or not. Changed the example to `[publisher]`, which is simply not in the model. ########## grails-forge/grails-forge-core/src/main/resources/gsp/index.gsp: ########## @@ -568,30 +568,30 @@ [code: 'welcome.binding.formatted', beans: applicationContext.getBeansOfType(grails.databinding.converters.FormattedValueConverter)], [code: 'welcome.binding.structured', beans: applicationContext.getBeansOfType(grails.databinding.TypedStructuredBindingEditor)], [code: 'welcome.binding.listeners', beans: applicationContext.getBeansOfType(grails.databinding.events.DataBindingListener)]]}"/> - <g:set var="numBindingBeans" value="${bindingGroups.sum { g -> g.beans.size() } ?: 0}"/> - <g:set var="mimeTypeProviders" + <g:def type="int" var="numBindingBeans" value="${(int) (bindingGroups.sum { g -> g.beans.size() } ?: 0)}"/> Review Comment: `g:set` takes a `type` now (`a794e2a`): the declaration is written first and the tag is still called, so the scope write happens, `scope`/`bean` are unaffected, and untyped `g:set` is unchanged. `type` is only valid alongside `value` — with a body or `bean` there's no expression to declare from. `g:def` needed the autocast too: it emitted `int.cast(...)` for a primitive, which throws whatever it's handed, so a page compiled and then failed to render. Casts through the wrapper now (`43dcdc4`). The welcome page stays on `g:def` — those twelve values are read only by the page that declares them, so the scope write is unused. Say the word if you'd rather it demonstrate `g:set type=` instead. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
