Spring MVC interview questions for experienced
Spring MVC interview questions for experienced
What is Spring Web MVC framework?
Spring Web MVC is open source web application framework which is part of the Spring framework. Spring Web MVC is one of the efficient and high performance implementation of Model View Controller (MVC) Architecture. Spring MVC provides utility classes to develop web applications.
What is DispatcherServlet?
Spring Web MVC framework is designed around the front controller which is DispatcherServlet. DispatcherServlet listens to the incoming requests and dispatches the request to handler, configurable handler mapping, view resolvers, local and theme resolvers.
DispatcherServlet is responsible for initializing the Spring Web MVC framework. It has to been configured in web.xml
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet>
What is Handler Mapping?
Handler mapping is responsible for mapping incoming web requests to appropriate handler that can process the request. DispatcherServlet delegates the request to Handler Mapping to inspect the request and return appropriate HandlerExecutionChain that can handle the request.
What are the different built in Handler Mapping implementations available in Spring 3?
The following are the different built in Handler Mapping implementations
What is SimpleUrlHandlerMapping?
SimpleUrlHandlerMapping is one of the implementation of Handler Mapping interface. It is most commonly used handler mapping. In this, we have to configure the incoming request to handler by bean id or name in the configuration file.
We have to set the mapping property of the SimpleUrlHandlerMapping with key as the request and value as the handler
The request ‘<url>/saveEmployee.htm’ will be handled by SaveEmployeeController and
‘<url/deleteEmployee.htm>’ will be handled by deleteEmployeeController.
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/saveEmployee.htm">saveEmployeeController</prop> <prop key="/deleteEmployee.htm">deleteEmployeeController</prop> </props> </property> </bean> <bean id="saveEmployeeController" class="com.answersz.employee.SaveEmployeeController" /> <bean id="deleteEmployeeController" class="com.answersz.employee.DeleteEmployeeController" />
What is ControllerClassNameHandlerMapping?
ControllerClassNameHandlerMapping is one of the implementation of Handler Mapping interface. In this mapping, the name of the controller itself acts as the request. The following are the conventions need to following
- convert the Controller name to lower case
- remove the controller suffix if exist
- add / as prefix
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> <bean class="com.answersz.employee.SaveEmployeeController" /> <bean class="com.answersz.employee.DeleteEmployeeController" />
According to the above configuration the request with ‘<url>/saveemployee’ will be handled by SaveEmployeeController and request with ‘<url>/deleteemployee’ will be handled by DeleteEmployeeController
What is a ModelAndView?
ModelAndView is an object which can hold both the model and view. The handler returns the ModelAndView object and DispatcherServlet resolves the view using View Resolvers and View. The View is an object which contains view name in the form of the String and model is a map to add multiple objects.
In the below example ‘DeleteEmployee’ is the view name.
ModelAndView model = new ModelAndView("DeleteEmployee"); model.addObject("employeeId", 21); model.addObject("msg", "Employee Deleted"); return model;
What is a ViewResolver?
DispatcherServlet uses the ViewResolver for mapping and dispatching the request to an appropriate view. ModelAndView object contains the logical view name. In the previous example ‘DeleteEmployee’ is the logical view name. DispatcherServlet will map the logical view name to view using ViewResolver.
What are the different built in ViewResolver implementations available in Spring 3?
Following are built in ViewResolver implementations available in Spring 3
What is Controller in Spring MVC?
Controller handles the incoming request and sends the data to Business layer (Service layer) to process the request.
Spring basic controller is org.springframework.web.servlet.mvc.Controller interface. The interface has one method and it is responsible for handling the request and return the ModelAndView Object
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
What are the different built in Controller implementations available in Spring Web MVC?
Following are built in Controller implementations available in Spring Web MVC