Spring MVC is mostly used with Spring for any web application development. It is very powerful and nice layered architecture for flow and configuration. It is very flexible to integrate with other web frameworks like struts. In interviews mostly flow of Spring MVC framework and configuration files are asked. Details on Spring core modules and Spring framework interview questions.
Flow of the Spring MVC:
These are major benefits of Spring MVC :-
2) Explain Servlet life cycle methods ?
Servlet life cycle is managed in these 4 steps and these methods need to be implemented for servlet to perform action.
1. Instantiation : It is done by servlet processor using Class.forName().
2. Initialization: It is done by init();
public void init(ServletConfig config) throws ServletException
3. Servicing the Request/Implementation : This is done by service() method and if you are not implementing service method then it calls doGet() and doPost() and it is depends on type of request.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
4. Destruction
public void destroy()
Spring provides a Dispatcher Servlet called DispatcherServlet, which maps to an URI path pattern, in this case it is /myApp/*. You configure the servlet in web.xml.
Flow of the Spring MVC:
- The DispatcherServlet configured in web.xml file receives the request.
- The DispatcherServlet finds the appropriate Controller with the help of HandlerMapping and then invokes associated Controller.
- Then the Controller executes the logic business logic (if written by the programmer) and then returns ModeAndView object to the DispatcherServlet.
- The DispatcherServlet determines the view from the ModelAndView object.
- Then the DispatcherServlet passes the model object to the View.
- The View is rendered and the Dispatcher Servlet sends the output to the Servlet container. Finally Servlet Container sends the result back to the user.
Sample Web.xml
Controller
Controllers are annotated with @Controller. The Spring MVC Dispatcher Servlet scans for all the conroller classes. There is no need to define the controllers as beans in the conext xml file, Spring automatically does that for us. All we have to do is adding one line in the context file
web-application-config.xml, which is passed in as an initialization paramter to the Spring MVC Dispatcher Servlet.
Sample Controller class:
Above is an example of a controller. URI path /mvc/* is mapped to this controller. Each handler method further narrows it down by additional URI path pattern and http method. For helloWorld() method maps to /mvc GET method. Handler methods have flexible signature and return type. In this example, they return three types:
Model in Spring MVC is actually a map, which contains beans (map values) and bean names (map keys). Model is completely separated from view and view rendering technology.
A special type of model is Form Model. Form model is specified in the form tag as following:
In the handler method, form model object can be passed in as method parameter, or returned as return value. By convention-over-configuration, Spring figures out form model object name by its class name. You can always use @ModelAttribute if the names does not match.
Spring Servlet xml
SpringMVC project folder structure
Controllers are annotated with @Controller. The Spring MVC Dispatcher Servlet scans for all the conroller classes. There is no need to define the controllers as beans in the conext xml file, Spring automatically does that for us. All we have to do is adding one line in the context file
web-application-config.xml, which is passed in as an initialization paramter to the Spring MVC Dispatcher Servlet.
Sample Controller class:
Above is an example of a controller. URI path /mvc/* is mapped to this controller. Each handler method further narrows it down by additional URI path pattern and http method. For helloWorld() method maps to /mvc GET method. Handler methods have flexible signature and return type. In this example, they return three types:
- void: view is implied by RequestToViewNameTranslator
- string: view name
- Model object: view is implied by RequestToViewNameTranslator
Model in Spring MVC is actually a map, which contains beans (map values) and bean names (map keys). Model is completely separated from view and view rendering technology.
A special type of model is Form Model. Form model is specified in the form tag as following:
In the handler method, form model object can be passed in as method parameter, or returned as return value. By convention-over-configuration, Spring figures out form model object name by its class name. You can always use @ModelAttribute if the names does not match.
Spring Servlet xml
Interview Questions:
1) Benefits of Spring MVC over Struts?These are major benefits of Spring MVC :-
1. We have clear separation between models,
views and controllers in Spring.
2. It is very versatile and flexible based on
interfaces but Struts forces Actions and Form object into concrete inheritance.
3. It provides both interceptors and
controllers, thus helps to factor out common behavior to the handling of many
requests.
4. It can be configured with different view
technologies like Freemarker, JSP, Tiles, Velocity, XLST etc. and also you can
create your own custom view mechanism by implementing Spring View interface.
5. In Spring MVC Controllers can be configured
using DI (IOC) that makes its testing and integration easy.
6. Web tier of Spring MVC is easy to test than
Struts web tier, because of the avoidance of forced concrete inheritance and
explicit dependence of controllers on the dispatcher servlet.
7. Spring web layer built on top of a business
object layer, which is considered a good practice. In Struts framework you need
to implement your business objects.
8. Struts force your Controllers to extend a Struts class but Spring
doesn’t, there are many convenience Controller implementations that you can
choose to extend.
2) Explain Servlet life cycle methods ?
Servlet life cycle is managed in these 4 steps and these methods need to be implemented for servlet to perform action.
1. Instantiation : It is done by servlet processor using Class.forName().
2. Initialization: It is done by init();
public void init(ServletConfig config) throws ServletException
3. Servicing the Request/Implementation : This is done by service() method and if you are not implementing service method then it calls doGet() and doPost() and it is depends on type of request.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
4. Destruction
public void destroy()
Thank you for this Information !!
ReplyDeleteLINUX INTERVIEW QUESTIONS
Linux FTP vsftpd Interview Questions
SSH Interview Questions
Apache Interview Questions
Nagios Interview questions
IPTABLES Interview Questions
Ldap Server Interview Questions
LVM Interview questions
Sendmail Server Interview Questions
Read more at Linux Troubleshooting
This comment has been removed by the author.
ReplyDeleteSpring MVC Examples Well Explain
ReplyDeletehttp://www.javaproficiency.com/2015/04/spring-mvc-example.html
Java Spring Online Training
ReplyDeleteSimple Nice one
ReplyDelete
ReplyDeleteGreat Blog Thanks.
Here You Can Find Your First Round Intreview question For Job .
Spring MVC Interview Questions
nice one.. thanks for sharing
ReplyDeletejob oriented java training | java training course contents | java training topics | best java j2ee training institute in chennai
This comment has been removed by the author.
ReplyDeleteThank you for the informative post. It was thoroughly helpful to me. Keep posting more such articles and enlighten us.
ReplyDeleteWeb Designing Training Course in Chennai | Certification | Online Training Course | Web Designing Training Course in Bangalore | Certification | Online Training Course | Web Designing Training Course in Hyderabad | Certification | Online Training Course | Web Designing Training Course in Coimbatore | Certification | Online Training Course | Web Designing Training Course in Online | Certification | Online Training Course