Google News
logo
Java Spring MVC Interview Questions
The Spring web MVC framework facilitates model-view-controller (MVC) architecture and ready components that can be used to develop flexible and loosely coupled web applications.
 
The MVC pattern separates the different aspects of an application (for example, input logic, business logic, and UI logic) and also enables loose coupling between model, view and controller within the application.
The front controller is a DispatcherServlet class present in org.springframework.web.servlet package. It dispatches the request to the appropriate controller and manages the flow of the application. It is required to specify the DispatcherServlet class in the web.xml file.
The following are the advantages of Spring MVC Framework :
 
* Separate roles : The Spring MVC separates the application into three interconnected layers where each layer has its role.

* Light-weight : It uses light-weight servlet container to develop and deploy your application.

* Powerful Configuration : It provides a robust configuration for both framework and application classes that includes easy referencing across contexts, such as from web controllers to business objects and validators.

* Rapid development : The Spring MVC facilitates fast and parallel development.

* Reusable business code : Instead of creating new objects, it allows us to use the existing business objects.

* Flexible Mapping : It provides the specific annotations that easily redirect the page.
Spring MVC implements a clear separation of concerns that allows us to develop and unit test our applications easily.
 
The concepts like :
 
* Dispatcher Servlet
* Controllers
* View Resolvers
* Views, Models
* ModelAndView
* Model and Session Attributes

are completely independent of each other, and they are responsible for one thing only.
 
Therefore, MVC gives us quite big flexibility. It's based on interfaces (with provided implementation classes), and we can configure every part of the framework by using custom interfaces.
 
Another important thing is that we aren't tied to a specific view technology (for example, JSP), but we have the option to choose from the ones we like the most.
 
Also, we don't use Spring MVC only in web applications development but in the creation of RESTful web services as well.
The Spring MVC application contains an additional configuration file that contains the properties information. This file can be created either in the form of an xml file or properties file. In this file, we generally define the base-package and view resolver where DispatcherServlet searches for the controller classes and view components path. However, it can also contain various other configuration properties.
The InternalResourceViewResolver is a class which is used to resolve internal view in Spring MVC. Here, you can define the properties like prefix and suffix where prefix contains the location of view page and suffix contains the extension of view page.
 
For example :
 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/"></property>  
        <property name="suffix" value=".jsp"></property>          
</bean>

 

The Model interface defines a holder for model attributes. The ModelMap has a similar purpose, with the ability to pass a collection of values. It then treats those values as if they were within a Map. We should note that in Model (ModelMap) we can only store data. We put data in and return a view name.
 
On the other hand, with the ModelAndView, we return the object itself. We set all the required information, like the data and the view name, in the object we're returning.
* AOP module (Aspect Oriented Programming)
* JDBC abstraction and DAO module
* The Core container module
* MVC framework module
* Application context module
* O/R mapping integration module (Object/Relational)
* Web module
Spring MVC framework is request-driven and is designed around a central Servlet that handles all the HTTP requests and responses. The DispatcherServlet, however, does a lot more than just that. It seamlessly integrates with the IoC container and allows you to use each feature of Spring.

On receiving an HTTP request, the DispatcherServlet consults HandlerMapping (these are the configuration files) to call the appropriate Controller. Then, the controller calls appropriate service methods to set the Model data. It also returns the view name to DispatcherServlet. DispatcherServlet, with the help of ViewResolver, picks up the defined view for the request. Once the view is finalized, the DispatcherServlet passes the Model data to View – where it is finally rendered on the browser.
A front controller is a controller which handles all requests for a Web application. When it comes to Spring MVC, DispatcherServlet is that front controller. When a web request is sent to a Spring MVC application, the DIspatcherServlet takes care of everything. First, it takes the request. Then, it organizes the different components like request handlers, controllers, view resolvers, and such – all needed to handle the request. And finally, it renders the content on the browser.