Saturday, 6 April 2019

How Spring MVC flow with interview question and answer

Workflow of a SpringMVC Application:-
...............................................................
1.Whenever client make a request that will be traped by the DispatcherServlet.

2.DispathcerServlet reads the bean configuration file then it takes the support of  HandlerMappings to idetify the Controller classes.

**HandlerMappings are predefined java classes those were given by the Spring..

**We generally configure HandlerMappings within a bean configuration file....

**HandlerMappings containes a information about the controller classes..

**Inorder to process each and every client request we generally maintain seperate controller classes.....

3.Finally DispatcherServlet invokes the Controller class based on the client request with the help of a HandlerMapping..

4.Controller class contains a complete information about a client in the form a CommandObject.....

5.Controller class will interacts with the model calsses ......

6&7.Based on the results those were return by the ModelClasses,Controller class returns a ModelAndView object to the DispatherServelet..

ModelAndView object cotnains a information about model class returned infomation and the presentation page....

8.DispatherServlet takes the support of a ViewResolver to identify the presentation page....

Actually a ModelAndView object may contain a logical inforamtion about the presentation page or It may cotain exact information about a presentaion page....

If the ModelAndView object contains a LogicalInforamation about the presentation page then DispatherServlet takes the support of a ViewResolver ....

If the ModelAndView object contains a ExactInforamation about the presentation page then DispatherServlet directly invokes the presentaion page without taking the support of a ViewResolver ......

=>Generaly we can return ModelAndView by specifiying the "LogicalName" of the jsp page
or
By Specifiying the exact name of the JspPage.....

................***................***................***........................***.............

Understanding the HandlerMapping's:-
..............................................................
=>HandlerMapping's are predefined javaclasses those were given by the Spring...

=>We can configure them as a SpringBean within a bean configuration file....

=> HandlerMapping's containes a information about the Controler classes.....

=>HandlerMapping's can able to idetifie the Controller classes based on the client request....

The predefined HandlerMapping's those were given by the Spring are.......

=>BeanNameUrlHandlerMapping
=>SimpleUrlHandlerMapping
=>CommonsPathMapUrlHandlerMapping
=>ControllerClassNameUrlHandlerMapping




------------------------------------------------------------------------------------------------------------------

Understanding a "ModelAndView" Object:-
....................................................................
=>The org.springframework.web.servlet.ModelAndView object is a predefined java class that was given by the Spring....

=>The ModelAndView object contains a information about the response page and the module class generated result....

=>The ModelAndView object can be created in two ways

**By Specifying the exactname of a ResponsePage...
or
**By Specifying the logicalname of a ResponsePage...

public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
{
.........
.......
return new ModelAndView("Success.jsp");
}


Case 2:-

public ModelAndView handleRequest(....)
{
.........
.......
List<String> list=new ArrayList<String>();
list.add("Ashok");
list.add("Mahesh");
........
......
return new ModelAndView("Success","listOfNames",list);
}

=>Here the "list" object is internally stored within a Request scope with keyname "listOfNames".....

=>Here we are returning ModelAndView object by specifieng the logical name of the response page....


----------------------------------------------------------------------------------

Understanding ViewResolver:-
.................................................
=>ViewResolver is used to invoke the response page....

=>Whenever a ModelAndView object return's a logical name to the DispatcherServlet,the DispatcherServlet takes the support of ViewResolver to identify the response page.....

=>ViewResolver is an interfce one of the implemetation class of a ViewResolver is InternalResourceViewResolver...

=>We generally configure the InternalResourceViewResolver into a bean configuration file......

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

=>The InternalResourceViewResolver adds "/" as a prefix and ".jsp" as suffix to logicalname..

Note:-
..........
=>If we are returning a ModelAndView object with a logical name we must and should configure a ViewResolver within a bean configuration file......

return new ModelAndView("Success");


=>If we are returning a ModelAndView object with a Exact jsp name we  should not configure a ViewResolver within a bean configuration file......

return new ModelAndView("Success.jsp");


=>If save the bean configurationfile name otherthan dispatcherservletname-servlet.xml we should configure within a web.xml using <context-param> but if we take the support of a <context-param> we should configure the "ContextLoadListner" class.....

So that we can make this as easy by configuring the bean configuration file as <init-param>(Initialization parameter)...

If we configure as a initialization parameter we no need to configure any explicit listener classes.....

ForEx:-
......

Bean configuration file: login-servlet.xml


<web-app>
<servlet>
<servlet-name>ds</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/login-servlet.xml</param-value>
</init-param>
</servlet>
.....
.....
If there are miltiple bean configuration file we can seperate with them using ","

Here the "contextConfigLocation" is a predefined initialization parameter.....



To compile the Above Application

we need to set
=>web.jar
=>web.servlet.jar file   as a class path..
=>servlet-api.jar

-----------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment