Running Vaadin on Grails
Grails is a web application framework, which uses the Groovy language, following the coding by convention paradigm. All the information about Grails can be found at the following link:
The Grails plugin called vaadin
integrates Vaadin into the Grails project. Therefore, we can use Vaadin as a replacement for the Grails view layer. Instead of writing HTML, CSS, and JavaScript, we make Vaadin view in Groovy. More information about the Vaadin integration is available at the following web page:
We are going to make a simple Vaadin application that will be running on Grails and written in Groovy.
Getting ready
Install the Eclipse Groovy/Grails Tool Suite (GGTS). download link is available at the Grails pages at http://grails.org/products/ggts.
Tip
If you are running on Windows, then install GGTS to a folder that does not contain white spaces (for example, C:\EclipseSTS
).
How to do it...
Carry out the following steps in order to create a new Grails project with Vaadin:
Open File | New | Grails Project.
Fill in the name of the project and finish the New Grails Project wizard.
Click on the Grails icon, which opens the console that we use for running the Grails command. In this step, we just want to make sure the project is created properly. Run the
run-app
command.The Grails application is opened up in the browser window using
http://localhost:8080/vaadin-in-grails
. It still uses.gsp
files, which we replace with Vaadin in the next step.Install the Vaadin plugin. Open up the Grails console and run the following command:
install-plugin vaadin
Mark the
grails-app/vaadin
folder as the source folder.Run the application again and open
http://localhost:8080/vaadin-in-grails
in the browser. Vaadin has replaced the Grails view.
How it works...
We have made a Grails project where we have replaced the Grails view layer with a Vaadin framework. The integration between Grails and Vaadin is done by the Grails plugin that is connecting these two frameworks. As you have experienced, the Vaadin plugin is available via the Grails plugin system (http://grails.org/plugin/vaadin).
Let's have a close look at what the Vaadin plugin has generated for us.
The MyUI.groovy
file has been generated inside the grails-app/vaadin
source folder. MyUI
represents the Vaadin user interface, which is shown to the user in the browser window.
package app import com.vaadin.ui.UI import com.vaadin.ui.VerticalLayout import com.vaadin.server.VaadinRequest import com.vaadin.ui.Label import com.vaadin.grails.Grails class MyUI extends UI { @Override protected void init(VaadinRequest vaadinRequest) { VerticalLayout layout = new VerticalLayout() String homeLabel = Grails.i18n("default.home.label") Label label = new Label(homeLabel) layout.addComponent(label) setContent(layout) } }
We should store all the
Vaadin code we create inside the grails-app/vaadin
source folder.
The Vaadin plugin needs to have information about the location of the MyUI
class in order to add that information to the web.xml
deployment descriptor. Therefore, a new file VaadinConfig.groovy
has been generated inside the grail-app/conf
folder.
vaadin { mapping = [ "/*": "app.MyUI" ] productionMode = false } environments { production { vaadin { productionMode = true } } }
In VaadinConfig.groovy
, we can change the path to the UI class. We can also add more UI classes.
vaadin { mapping = [ "/*": "app.MyUI", "/ui/*": "app.YourUI" ]
The last thing which has been done during the plugin installation is the removal of the URL mapping inside the UrlMappings.groovy
file.
Tip
If we make changes in the code, then the code is recompiled and we don't have to restart the server (just refresh the web page).
Changes on class level (for example, a change of parent class) are not recompiled and we have to restart the application.
See also
More about Grails can be learned from the manual at http://grails.org/doc/latest
We will explore Grails and Vaadin integration in Chapter 8, Spring and Grails Integration