Using Grails services in Vaadin

We will make a bit of an interactive application in this recipe. A client will be able to add new users into the database and the user interface will reflect the actual state of the User database table.

The following screenshot shows what we are going to develop in this recipe:

Using Grails services in Vaadin

How to do it...

Carry out the following steps, in order to use Grails services inside Vaadin application:

  1. Create a new domain class called User inside the grails-app/domain folder.
    class User {
    
      String name
    
      static constraints = {
      }
    }
  2. Create a new service for the User class. We will implement the getAll method that returns all the users from the database and the add method that stores a new user in the database. The following screenshot shows how to make the service in Eclipse:
    How to do it...

    Now type the name of the domain class we want to make the service for.

    How to do it...

    The following code shows what has been generated by the Grails framework. Add the getAll and add methods into the UserService class.

    class UserService {
    
      List<User> getAll() {
        List<User> list = User.list()
        return list
      }
    
      User add(String name) {
        User user = new User(name: name)
        user.save(failOnError:true)
        return user
      }
    }
  3. Create a new click listener that will take care of creating new users by using the add method from UserService.
    public class AddUserListener implements ClickListener {
    
      @Override
      public void buttonClick(ClickEvent event) {
        MyUI ui = (MyUI) UI.getCurrent()
        String value = ui.txtName.getValue()
        User user = Grails.get(UserService).add(value)
        ui.layout.addComponent(new Label(user.name))
      }
    }
  4. Now add the needed components in order to be able to add and display the users in the browser.
    class MyUI extends UI {
    
      TextField txtName = new TextField("Name: ");
       VerticalLayout layout = new VerticalLayout()
    
      @Override
      protected void init(VaadinRequest vaadinRequest) {
        layout.margin = true
        setContent(layout)
    
        layout.addComponent(txtName);
    
        Button btnAdd = new Button("Add");
        layout.addComponent(btnAdd);
        btnAdd.addClickListener(new AddUserListener());
    
        List<User> users = Grails.get(UserService).getAll()
        for (User user : users) {
          layout.addComponent(new Label(user.name))
        }
      }
    }

How it works...

Creating the persistent classes and related services is pretty easy and quick in Grails. Grails services are transactional by default. We can switch them off by adding the transactional static field into the UserService class.

static transactional = true

Alternatively, we can configure it via the @Transactional annotation from Spring.

More information about Grails services can be found at http://grails.org/doc/latest/guide/services.html.

Dependency injection in Vaadin is done via the get() method from the Grails class. The Grails class is provided by the Vaadin plugin for the Grails framework. The Grails class uses the Spring application context for obtaining the service instances.

Note that we should not make strong references to Grails services in Vaadin classes. Instead, use the Grails.get() method. Obtaining classes from the Spring application context is fast and we don't have to worry about performance. The reason to do so is that strong references (instance variables) might be serialized together with Vaadin components and after the services are deserialized, they would not work properly, because they would be out of Spring's control.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.169