Request Object and Spring DataBinders


I am fan of spring framework and I love to use it all the times. In this post I am going to show you how you can bind the data from ServletRequest object to any Java Bean (POJO). Let’s say you have a spring web application and you are working on piece of code which is not directly interacting with Web layer (i.e. don’t have direct access of Request and Response Objects), so 2 questions rises here are:

1) How can I get access of my current request object in plain java class which is not a web component?
2) How can I bind the parameters in request object to my POJO bean?

  • Request Object access in non web classes
There are ways with which you can make you springs beans aware of ServletRequest object by implementing the interface ServletRequestAware (interface from opensymphony-webwork). But, there is another easier way of doing it with the help of spring Utility classes.

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

  • Now have the request object and let’s try to bind it to a POJO.
Spring DataBinders is a simple answer. There are many out of box implementations are available for various type of data binding operations. One of the data binders which springs has is ServletRequestDataBinder, spring internally uses this binder to wrap a POJO (command class) with the values from request object. So, let say you have a “User” class with 2 instance variables “username” and “password” also, you have a form which contains many text boxes (let’s say 30) and two of them are for userName and password. Now, in normal scenario we have a command class assigned to a controller and spring automatically populates the properties/instance variables in command class. Let’s say we want to populate our command/POJO manually using the request object in some other class (non-controller class), how to do that? Here is the code

class User {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

class MyNonControllerClass {
    private User user;

    public MyNonControllerClass() {
        user = new User();
        //We can write this code in more better place (apart from constructor) based on need.
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        ServletRequestDataBinder binder = new ServletRequestDataBinder(user);
        binder.bind(request);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

I hope this handy trick will help you to utilize the request objects and spring binders at many places in your code. Enjoy!

Comments

Popular posts from this blog

AEM, FORM Submission & Handling POST requests

AEM - Query list of components and templates

AEM 6.3 - Bundle Whitelisting - Deprecation of administrative authentication

AEM as a Cloud Service (AEMaaCS) – Architecture Overview