We can group URL mappings defined in grails-app/conf/UrlMappings.groovy
using the group()
method defined for the URL mapping DSL. The first argument is the first part of the URL followed by a closure in which we define mappings like we are used to.
Suppose we have defined the following two mappings in our UrlMappings.groovy
file, both starting with /admin:
Continue reading →
Since Grails 2.3 we can use the url-mappings-report
command to get a nice report of the URL mappings we have defined in our application. Also implicit mappings created for example by using the resources
attribute on a mapping definition are shown in the report. This report is very useful to see which URLs are exposed by your application and how they map to controllers.
Suppose we have the following grails-app/conf/UrlMappings.groovy
with a couple of mappings:
Continue reading →
In Grails we can convert a request parameter to a type directly. We must then use the int()
, short()
, byte()
, long()
, double()
, float()
, boolean()
or list()
methods that are added to the params
object available in our controllers.
Since Grails 2.3 we can also pass a default value, which is used when the request parameter is not set. In the following controller we use the double()
method and define a default value of 42.0
.
Continue reading →
Grails has a built-in URL constraint to check if a String value is a valid URL. We can use the constraint in our code to check for example that the user input http://www.mrhaki.com is valid and http://www.invalid.url is not. The basic URL validation checks the value according to standards RFC1034 and RFC1123. If want to allow other domain names, for example server names found in our internal network, we can add an extra parameter to the URL constraint. We can pass a regular expressions or a list of regular expressions for patterns that we want to allow to pass the validation. This way we can add IP addresses, domain names and even port values that are all considered valid. The regular expression is matched against the so called authority part of the URL. The authority part is a hostname, colon (:
) and port number.
In the following sample code we define a simple command object with a String property address
. In the constraints
block we use the URL constraint. We assign a list of regular expression String values to the URL constraint. Each of the given expressions are valid authorities, we want the validation to be valid. Instead of a list of values we can also assign one value if needed. If we don't want to add extra valid authorities we can simple use the parameter true
.
Continue reading →
In Grails we can define properties for services, controller, taglibs, Spring components and other components in the Spring application context through configuration. This is called property overriding in Spring terminology. This feature is very useful to define or override property values on components in the application context. We can define the property values in a beans{}
block in Config.groovy
. We can also load external configuration files, like property files and define property values in those.
With property overriding we don't have to look for property values via an injected GrailsApplication
object and the config
property of GrailsApplication
. The code using the property value is now much cleaner and easier to test.
Continue reading →
Since Grails 2 we can render binary output with the render()
method and the file
attribute. The file
attribute can be assigned a byte[]
, File
, InputStream
or String
value. Grails will try to determine the content type for files, but we can also use the contentType
attribute to set the content type.
In the following controller we find an image in our application using grailsResourceLocator
. Then we use the render()
method and the file
and contenType
attributes to render the image in a browser:
Continue reading →
There is really no excuse to not write unit tests in Grails. The support for writing tests is excellent, also for testing code that has to deal with the locale set in a user's request. For example we could have a controller or taglib that needs to access the locale. In a unit test we can invoke the addPreferredLocale()
method on the mocked request object and assign a locale. The code under test uses the custom locale we set via this method.
In the following controller we create a NumberFormat
object based on the locale in the request.
Continue reading →
Since Grails 2.1 we can create a Grails wrapper. The wrapper allows developer to run Grails commands in a project without installing Grails first. The wrapper concept is also available in other projects from the Groovy ecosystem like Gradle or Griffon. A wrapper is a shell script for Windows, OSX or Linux named grailsw.bat
or grailsw
and a couple of JAR files to automatically download a specific version of Grails. We can check in the shell scripts and supporting files into a version control system and make it part of the project. Developers working on the project simply check out the code and execute the shell script. If there is no Grails installation available then it will be downloaded. To create the shell scripts and supporting files someone on the project must run the wrapper command for the first time. This developer must have a valid Grails installation. The files that are generated can then be added to version control and from then one developers can use the grailsw
or grailsw.bat
shell scripts.
$ grails wrapper
| Wrapper installed successfully
$
Continue reading →
We can configure Spring beans using several methods in Grails. We can for example add them to grails-app/conf/spring/resources.xml
using Spring’s XML syntax. But we can also use a more Groovy way with grails-app/conf/spring/resources.groovy
. We can use a DSL to define or configure Spring beans that we want to use in our Grails application. Grails uses BeanBuilder to parse the DSL and populate the Spring application context. To define a bean we use the following syntax beanName(BeanClass)
. If we want to set a property value for the bean we use a closure and in the closure we set the property values. Let’s first create a simple class we want to configure in the Spring application context:
// File: src/groovy/com/mrhaki/spring/Person.groovy
package com.mrhaki.spring
import groovy.transform.ToString
@ToString
class Person {
String name
Date birthDate
}
Continue reading →