Spring MVC Flashcards
Explain MVC
MVC is web application architectural pattern that aims to reduce coupling between different aspects of web application.
MVC stands for Model-View-Controller.
Model - Model domain contains the business logic and functions that manipulate the business data. It is also called domain layer. It provides updated information to view layer and replies to the query. And the controller can access the functionality encapsulated in model.
View - View is concerned with the presentation aspect of the application according to the model data and also responsible to forward query response to controller.
Controller - Accepts and intercepts user requests and controls the business objects to fulfil these requests. it works as orchestrator to fulfil the request. A controller can talk with multiple business objects to complete request.
Which pattern does Spring MVC implement which listens for all the web requests and then forwards it to respective controllers?
FrontController. All requests first lant on this servlet which then forwards it to proper controller based on url mapping.
What does controller return and who processes that information.
Controller returns logical view name and then dispatcher servlet uses ViewResolver to resolve the logical name to actual view and passes the model to JSP.
What is the name of FrontController in spring?
DispatcherServlet.
Which class creates WebApplication context, etc?
DispatcherServlet creates web related context and view resolvers. It is then nested inside root application context so that web components can easily find their dependencies.
How is xml file for web application context located?
The dispatcher servlet will by default take name of its servlet from web.xml and search for name-servlet.xml in /WEB-INF. For example if servlet is named spring, then it will search for spring-servlet.xml
Which are mandatory configurations in web.xml for spring?
Servlet name
Servlet class - DispatcherServlet
Load on startup - 1
Servlet mapping
servlet name
url pattern -> /
mapping / to dispatcher servlet means all requests will be routed through dispatcher servlet.
Role of controllers in spring?
execute the actual request, prepare the model and select a view to render. Controller is the glue that glues logic and web interface.
Which annotation is used to desginate a class as controller
@Controller is used. Don’t forget to enable component scan in xml.
Which configuration is required for enabling of creation of controller and other stereotypes using annotation?
context:component-scan - property base package = “co.edureka.package”
So all the classes inside that package are scanned.
Which annotation is required to declare that a method will handle requests for a particular endpoint?
@RequestMapping is used. Need to provide endpoint in it.
@RequestMapping(value=”/home”, method=POST)
What are the objects that hold form values called?
Command objects. In form tag you need to configure command name which is the name of model.
How to do data binding in spring?
Need to include Spring form tag library which is integrated with Spring and provides tags access to command object and reference data your controller deals with.
Which jar contains Spring form tag library
spring-webmvc.jar contains it.
Sample spring form configuration
form:form action=”register” method=”post” commandName=”userForm”
form:input path=”username”
form:password path=”password”
input type=”submit” value=”Register”