Monday, September 11, 2017

Spring MVC : The Framework - Part 2

Spring-MVC Sample


As I described in the previous segment, spring-mvc is a framework that is really useful in developing web-applications. So, to demonstrate the abilities of Spring-MVC lest start with a sample application for library-management-system and understand the building boxes of Spring-MVC

Initializing Project

  1. In this sample project we will use 'Maven' as our dependency management tool. So, initialize a maven-project and add the dependency for 'spring-mvc' in the 'pom' file.
  2. Then Create a directory-structure as follows :


Configurations for Spring-MVC


First of all as I explained in the last chapter we have to map the servlet which our application going to fit. To that we have to create a 'web.xml' file inside 'spring-mvc-sample/src/main/webapp/WEB-INF' directory and add the following configurations to it : 



This configuration will tell the 'DispatcherServlet' to handle the requests coming from the configured URL pattern. After initializing the 'DispatcherSevlet' the frame-work will try to load the 'application-context' from a file named '[servlet-name]-servlet.xml' file which should also be located at 'webapp/WEB-INF' directory by default (to configure this spring-mvc has a mechanism which I will describe in the next chapter).  So, for the application-context to load add a file named 'spring-sample-servlet.xml' into spring-mvc-sample/src/main/webapp/WEB-INF' directory and add the following lines to it.


'context-component-scan' is the place Spring starts searching for the configurations. Usually we will give the 'group-ID' or the 'Base-Package' of our project as base-package. 

The next configuration is for the view-resolver which resolves views when a view-name is given. So, according to this configurations it will resolve views in the path 'WEB-INF/jsp' and with the '.jsp' prefix.

Then add a controller class 'HomeController' inside the 'spring-mvc-sample /src /main /java /com /springmvc /sample /controller' direcotory and add the following lines to it. 


@Controller annotation will tell that this class should be setved as 'Spring-MVC-Controller'. @RequestMapping annotation on top of the 'getHomePage' method will inform Spring where to map the request and according to which HTTP-method.  

And then create a 'home.jsp' file inside 'webapp/WEB-INF' directory and add sample HTML content into it.

And the sample app is done. You can checkout the full-project here














Sunday, September 10, 2017

Git Commit Messages

Why Messages ?



Git is a Version-Control system to track the  changes to the files in your computer. It is a tool to control and keep track of the changes to software products.  Git Commit  is where you put some specific changes to files into the Git Local Repository.  It is compulsory to put message when you do a Git-Commit. 

A message is a communication  medium. What are we going to communicate through a 'Git-Message' ? Is it really important to put a message to a commit ? To whom are we going to communicate ? 

When talking about git we always remember a software-project where multiple developers are working. So, a commit-message is going to communicate the group of developers about what has been changed in the project. In a software project a group of people works to achieve a common goal. So, it is really important to all of them to know what has happened to the project. But the message should be much more meaningful and descriptive. 

Some Standards


If we search internet we could find some standard ways to put git-messages. In this article I would cover some basic standards which I use. 

A change to the software project can be a new-feature, update to an existing feature or a bug-fix. So, if you to write a commit message it would be much easier to others if we could let them know to which category of above was the change belongs to. So, when you write a message always start it with either one of these three prefixes. 
  1. New : (if a change was a new feature)
  2. Fix : (if change is a bug fix)
  3. Update :  (if change is an update to an existing feature)
After the  prefix then put a semi-colon and write your message. After the message you have to convey whether the change is finished or not. If the change is finished put a full-stop (.) after the message. If the change is unfinished you can put a comma (,) and wip which stands for 'work in progress'. 

Eg : 
       Fix : Login Page changed.
       New : User registration validation added, wip

Saturday, September 9, 2017

Spring MVC : The Framework - Part 1

What is Spring-MVC ?


Spring-MVC is a framework that offers Model-View-Controller Architecture to build loosely coupled and flexible web-applications. MVC Pattern separates the concerns of Business-Logic, Model and UI components so that  can evolve themselves without distracting other components.  Lets take a look at what is actually Model-View-Controller in this architecture.

  • The Model encapsulates the application data and in general they will consist of POJO.
  • The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
  • The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.


Dispatcher-Servlet : What is happening ?


Whole Spring-MVC framework is build around the Dispatcher-Servlet  which handles HTTP Requests and HTTP Responses. It more likely to be the Global-Handler in Spring-MVC. The following diagram is to explain the request-handling-process of Spring-MVC.


After receiving an HTTP request, Front-Controller (DispatcherServlet) consults the handler-mappings and calls the appropriate controller. 

The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet. 

Then Front-Controller checks with the view-resolver to find out which 'view' it should render back to the User. When the 'View' is selected, it binds the Modal data to the View and renders it.