Data binding with Command Design pattern

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
- GOF Design Pattern

You learned about the Command Design pattern in Chapter 3, Consideration of Structural and Behavioral Patterns. It is a part of the Behavioral pattern family of the GOF pattern. It is a very simple data-driven pattern. It allows you to encapsulate your request data into an object, and pass that object as a command to the invoker method, and that method returns the command as another object to the caller.

Spring MVC implements the Command Design pattern to bind the request data from the web form as an Object, and passes that object to the request handler method in the controller class. Here, we will explore how to use this pattern to bind the request data to the Object, and also explore the benefits and possibilities of using data binding. In the following class, the Account java bean is a simple object with three properties--id, name, and balance:

    package com.packt.patterninspring.chapter10.bankapp.model; 
 
    public class Account{ 
    
     Long id; 
     Long balance; 
     String name; 
    
     public Long getId() { 
         return id; 
     } 
     public void setId(Long id) { 
         this.id = id; 
     } 
     public Long getBalance() { 
         return balance; 
     } 
     public void setBalance(Long balance) { 
           this.balance = balance; 
     } 
     public String getName() { 
         return name; 
     } 
     public void setName(String name) { 
         this.name = name; 
     } 
     @Override 
     public String toString() { 
         return "Account [id=" + id + ", balance=" + balance + ", name=" + name + "]"; 
     } 
          
    } 

Either we submit the web form with the input text fields' names the same as the Object properties' name, or we receive the request as http://localhost:8080/Chapter-10-Spring-MVC-pattern/account?id=10000. In both cases, behind the scenes, Spring calls the setter methods of the Account class to bind the request data or web form data to the object. Spring also allows you to bind indexed collections such as List, Map, and others.

We can also customize data binding. Spring provides these two ways to customize data binding:

  • Global Customization: It customizes the data-binding behavior across the web application for a particular Command Object
  • Per Controller Customization: It customizes the data-binding behavior per controller class for a particular Command Object

Here, I will discuss only the per controller customization. Let's see the following code snippet for customizing data binding for the Account object:

    package com.packt.patterninspring.chapter10.bankapp.web.controller; 
 
    .... 
    .... 
    @Controller 
    public class AccountController { 
    
     @Autowired 
     AccountService accountService; 
     .... 
     .... 
     @InitBinder 
     public void initBinder(WebDataBinder binder) { 
         binder.initDirectFieldAccess(); 
         binder.setDisallowedFields("id"); 
         binder.setRequiredFields("name", "balance"); 
     } 
     .... 
     .... 
    } 

As you can see in the preceding code, AccountController has a initBinder(WebDataBinder binder) annotated with the @InitBinder annotation. This method must have a void return type, and have an org.springframework.web.bind.WebDataBinder as a method argument. The WebDataBinder object has several methods; we have used some them in the preceding code. WebDataBinder is used to customize the data binding.

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

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