Archive: January 2016

Ratpacked: Special Routing Of Promise Values Using Predicates

Posted on by  
Hubert Klein Ikkink

One of the strengths of Ratpack is the asynchronous execution model. An important class is the Promise class. An instance of the class will represent a value that is available later. We can invoke several operations that need be applied to a value when a Promise is activated. Usually the activation happens when we subscribe to a Promise using the then method. We can use the route method for a Promise to have a different action when a certain predicate is true. The action will stop the flow of operations, so methods that are executed after the route method are not executed anymore if the predicate is true. If the predicate is false then those methods are invoked.

The Promise class has a method onNull as a shorthand for the route method where the predicate checks if the value is null. For example we could have a service in our application that returns a Promise. If the value is null we want some special behaviour like sending a 404 status code to the client. With the following code we could achieve this:

Continue reading →

Change Font Size With Mouse In IntelliJ IDEA

Posted on by  
Hubert Klein Ikkink

We can change the font size in our editor using shortcut keys in IntelliJ IDEA. But we can also use our mouse wheel to do this. We must enable this option in the settings of IntelliJ IDEA. We select the Preferences and then General | Editor. Here we select the option Change font size (Zoom) with Command+Mouse Wheel:

Continue reading →

Grails Goodness: Go To Related Classes In IntelliJ IDEA

Posted on by  
Hubert Klein Ikkink

Normally in a Grails application we have classes that are related to each other, but are located in different directories. For example a controller with several views. Or a Grails service with corresponding specifications. In IntelliJ IDEA we can use Choose Target and IDEA will show classes, files and methods that are relevant for the current file we are editing. The keybinding on my Mac OSX is Ctrl+Cmd+Up, but can be different on your computer and operating system. We can also choose the menu option Navigate | Related symbol....

In the following example we are editing the file MessagesController. We select the action Choose Target, IntelliJ IDEA shows a popup menu with the views for this controller and the specification class:

Continue reading →

Grails Goodness: Change Locale With Request Parameter

Posted on by  
Hubert Klein Ikkink

Grails has internationalisation (i18n) support built-in. It is very easy to add messages for different locales that can be displayed to the user. The messages are stored in properties files in the directory grails-app/i18n. Grails checks the request Accept-Language header to set the default locale for the application. If we want to force a specific locale, for example for testing new messages we added to the i18n property files, we can specify the request parameter lang. We specify a locale value and the application runs with that value for new requests.

The following screenshot shows a scaffold controller for a Book domain class with a default locale en:

Continue reading →

Grails Goodness: Enable Hot Reloading For Non-Development Environments

Posted on by  
Hubert Klein Ikkink

If we run our Grails 3 application in development mode our classes and GSP's are automatically recompiled if we change the source file. We change our source code, refresh the web browser and see the results of our new code. If we run our application with another environment, like production or a custom environment, then the reloading of classes is disabled. But sometimes we have a different environment, but still want to have hot reloading of our classes and GSP's. To enable this we must use the Java system property grails.reload.enabled and reconfigure the Gradle bootRun task to pass this system property.

Let's change our Gradle build file and pass the Java system property grails.reload.enabled to the bootRun task if it is set. We use the constant Environment.RELOAD_ENABLED to reference the Java system property.

Continue reading →

Ratpacked: Validating Forms

Posted on by  
Hubert Klein Ikkink

Ratpack is a lean library to build HTTP applications. Ratpack for example doesn't include functionality to validate forms that are submitted from a web page. To add form validation to our Ratpack application we must write our own implementation.

Let's write a simple application with a HTML form. We will use Hibernate Validator as a JSR 303 Bean Validation API implementation to validate the form fields. IN our application we also use the MarkupTemplateModule so we can use Groovy templates to generate HTML. We have a simple form with two fields: username and email. The username field is required and the email field needs to have a valid e-mail address. The following class uses annotations from Hibernate Validator to specify the constraints for these two fields:

Continue reading →

shadow-left