forked from grails/grails-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
whatsNew24.gdoc
261 lines (181 loc) · 8.67 KB
/
whatsNew24.gdoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
h4. Groovy 2.3
Grails 2.4 comes with Groovy 2.3 which includes many new features and enhancements.
For more information on Groovy 2.3, see the [comprehensive release notes|http://groovy.codehaus.org/Groovy+2.3+release+notes].
h4. Spring 4.0
Grails 2.4 comes with Spring 4.0.4 which includes many new features and enhancements. See the [Spring documentation|http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/new-in-4.0.html].
h4. Hibernate 4.3
Grails 2.4 now uses Hibernate 4.3.5 by default (Hibernate 3 is still available as an optional install).
h4. Standalone GORM and GSP
GORM and GSP can now be used outside of Grails. See the following guides / examples for more information:
* [Accessing Data with GORM|https://spring.io/guides/gs/accessing-data-gorm/]
* [Accessing MongoDB Data with GORM|https://spring.io/guides/gs/accessing-data-gorm-mongodb/]
* [GSP in Spring Boot Example Application|https://github.com/grails/grails-boot/tree/master/sample-apps/gsp/gsp-example]
h4. The Asset-Pipeline replaces Resources to serve static assets.
The asset-pipeline provides a new, easier to manage, faster means of managing your javascript, css, and images, while also bringing compiled client languages in to the fray as first class citizens (i.e. Coffeescript, LESS, SASS).
All your assets should now live in the @grails-app/assets@ subflolders. Three folders are made for you by default:
* javascripts
* stylesheets
* images
Now, defining manifests are done directly in your javascript files, or css by using require directives!
{code}
//= require jquery
//= require_self
//= require file_a
//= require_tree .
console.log('some javascript');
{code}
Easily add your assets to your GSP files:
{code}
<asset:javascript src="application.js"/>
<asset:stylesheet href="application.css"/>
<asset:image src="grails_logo.png" height="60" />
{code}
Enjoy developing with on the fly asset processing, asset compiling on WAR, and much more. See the [docs|http://bertramdev.github.com/asset-pipeline] for more info.
h4. Static Compilation
Groovy is a dynamically dispatched, dynamically typed language by default but also has great support for static type checking and static compilation. See [these notes on Groovy static compilation|http://docs.codehaus.org/display/GroovyJSR/GEP+10+-+Static+compilation]. In general Grails supports Groovy's static compilation but there are a lot of special situations which are common in a Grails app which cannot be statically compiled. For example, if a method marked with @\@CompileStatic@ contains code which invokes a GORM dynamic finder the code will not compile because the Groovy compiler cannot verify that the dynamic finder is valid. Grails 2.4 improves on this by allowing code to be staticaly compiled and still do things like invoke GORM dynamic finders.
The [grails.compiler.GrailsCompileStatic|api:grails.compiler.GrailsCompileStatic] annotation behaves much like the [groovy.transform.CompileStatic|api:groovy.transform.CompileStatic] annotation and provides special handling to recognize Grails specific constructs.
The following controller is marked with @\@GrailsCompileStatic@. All of the code that can be statically compiled will be statically compiled. When the compiler encounters code which can not be statically validated, normally that would result in a compile error. The Grails compiler will allow certain things to be considered valid and dynamically dispatch those instructions.
{code:java}
// grails-app/controllers/com/demo/PersonController.groovy
package com.demo
import grails.compiler.GrailsCompileStatic
@GrailsCompileStatic
class PersonController {
def showKids() {
def kids = Person.findAllByAgeLessThan(16)
// ...
}
}
{code}
There may be situations where most of the code in a class should be statically compiled but a specific method should be left to dynamic compilation. See the following example.
{code:java}
import grails.compiler.GrailsCompileStatic
import groovy.transform.TypeCheckingMode
@GrailsCompileStatic
class SomeClass {
def update() {
// this method will be statically compiled
}
@GrailsCompileStatic(TypeCheckingMode.SKIP)
def save() {
// this method will not be statically compiled
}
def delete() {
// this method will be statically compiled
}
}
{code}
The [grails.compiler.GrailsTypeChecked|api:grails.compiler.GrailsTypeChecked] annotation behaves much like the [groovy.transform.TypeChecked|api:groovy.transform.TypeChecked] annotation and provides special handling to recognize Grails specific constructs.
See the [static compilation and type checking|guide:staticTypeCheckingAndCompilation] section for more details.
h4. More Advanced Subqueries in GORM
The support for subqueries has been extended. You can now use @in@ with nested subqueries:
{code}
def results = Person.where {
firstName in where { age < 18 }.firstName
}.list()
{code}
Criteria and where queries can be seamlessly mixed:
{code}
def results = Person.withCriteria {
notIn "firstName", Person.where { age < 18 }.firstName
}
{code}
Subqueries can be used with projections:
{code}
def results = Person.where {
age > where { age > 18 }.avg('age')
}
{code}
Correlated queries that span two domain classes can be used:
{code}
def employees = Employee.where {
region.continent in ['APAC', "EMEA"]
}.id()
def results = Sale.where {
employee in employees && total > 100000
}.employee.list()
{code}
And support for aliases (cross query references) using simple variable declarations has been added to where queries:
{code}
def query = Employee.where {
def em1 = Employee
exists Sale.where {
def s1 = Sale
def em2 = employee
return em2.id == em1.id
}.id()
}
def results = query.list()
{code}
h4. GORM for Hibernate in Unit tests
It is no longer necessary to create integration tests in order to test GORM interactions with Hibernate. You can now instead use @HibernateTestMixin@:
{code}
...
import grails.test.mixin.hibernate.*
@TestMixin(HibernateTestMixin)
class PersonSpec extends Specification{
void setupSpec() {
hibernateDomain([Person])
}
void "Test count people"() {
expect:"Test execute Hibernate count query"
Person.count() == 0
sessionFactory != null
transactionManager != null
session != null
}
}
{code}
h4. Views For Namespaced Controllers
The views for namespaced controllers may now be defined in the @grails-app/views/<namespace name>/<controller name>/@ directory. See the [Models And Views|guide:modelsAndViews] section for more details.
h4. Improved Programmatic Transactions
Transaction attributes may now be specified when invoking @withTransaction@.
{code:java}
// the keys in the Map must correspond to properties
// of org.springframework.transaction.support.DefaultTransactionDefinition
Account.withTransaction([propagationBehavior: TransactionDefinition.PROPAGATION_REQUIRES_NEW,
isolationLevel: TransactionDefinition.ISOLATION_REPEATABLE_READ]) {
// ...
}
{code}
See the [withTransaction|domainClasses] docs for more information.
h4. New Maven Plugin
The Maven plugin has been rewritten to use [Aether for dependency resolution|http://wiki.eclipse.org/Aether/Using_Aether_in_Maven_Plugins] and can now be used with both Grails 2.3.x and Grails 2.4.x without releasing a new version of the plugin.
This means that the Maven plugin version number is no longer tied to the version number of Grails and new releases of the Maven plugin will not come out with each new Grails release. Instead, users can continue to use the 2.4.0 version of the plugin for any version of Grails going forward.
h4. Improved Unit Testing Support For allowedMethods
The allowedMethods property is now respected in unit tests.
{code:java}
// grails-app/controllers/com/demo/DemoController.groovypackage com.demo
class DemoController {
static allowedMethods = [save: 'POST', update: 'PUT', delete: 'DELETE']
def save() {
render 'Save was successful!'
}
// ...
}
{code}
{code:java}
// test/unit/com/demo/DemoControllerSpec.groovy
package com.demo
import grails.test.mixin.TestFor
import spock.lang.Specification
import static javax.servlet.http.HttpServletResponse.*
@TestFor(DemoController)
class DemoControllerSpec extends Specification {
void "test a valid request method"() {
when:
request.method = 'POST'
controller.save()
then:
response.status == SC_OK
response.text == 'Save was successful!'
}
void "test an invalid request method"() {
when:
request.method == 'DELETE'
controller.save()
then:
response.status == SC_METHOD_NOT_ALLOWED
}
}
{code}