Spring MVC and the Web Layer Flashcards
MVC is an abbreviation for a design pattern. What does it stand for and what is the idea
behind it?
MVC stands for Model-View-Controller software architectural pattern. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
Do you need spring-mvc.jar in your classpath or is it part of spring-core?
Spring Web MVC related OOTB beans are contained in two main modules:
* spring-web.jar
* spring-webmvc.jar
They need to be added to classpath independently and are not part of the spring-core.jar. There is also no such module as spring-mvc.jar.
What is the DispatcherServlet and what is it used for?
The DispatcherServlet is an entry point for every Spring Web application. It dispatches requests to handlers, with configurable handler mappings, view resolution, locale, timezone, and support for uploading files. The DispatcherServlet converts HTTP requests into commands for controller components and manages rendered data as well.
Following is the sequence of events corresponding to an incoming HTTP request to DispatcherServlet −
- After receiving an HTTP request, DispatcherServlet
consults the HandlerMapping to call the appropriate
Controller. - The Controller takes the request and calls the
appropriate service methods based on used GET or
POST method. The service method will set model
data based on defined business logic and returns
view name to the DispatcherServlet. - The DispatcherServlet will take help from
ViewResolver to pickup the defined view for the
request. - Once view is finalized, The DispatcherServlet
passes the model data to the view which is finally
rendered on the browser.
Is the DispatcherServlet instantiated via an application context?
The DispatcherServlet is NOT instantiated via an application context. The DispatcherServlet creates a separate “servlet” application context (also called Web Context or the DispatcherServletContext) itself. It is considered good practice to define MVC related beans ( i.e. controllers and view resolvers) in DispatcherServletContext.
What is the root application context? How is it loaded?
The ApplicationContext (also called RootApplicationContext) contains all non-web beans and is being instantiated using bean of type ContextLoaderListener. The relationship between the two contexts is hierarchical (root context is the parent), thus, DispatcherServletContext can access all beans from RootApplicationContext but not vise-versa.
a. ContextLoaderListener creates a root web-application-context for the web-application and puts it in the ServletContext.
b. DispatcherServlet creates its own WebApplicationContext and the handlers/controllers/view-resolvers are managed by this context.
c. A child-context created by DispatcherSerlvet is attached to the root application-context.
What is the @Controller annotation used for? How can you create a controller without an
annotation?
The @Controller annotation defines the class as a Spring MVC controller. The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.
You can create controller without annotation by creating a class that implements Controller interface and defining it as a bean in DispatcherServletContext with a name matching the URL mapping of the corresponding dispatcher servlet. But this functionality is deprecated and should not be used anymore.
What is the ContextLoaderListener and what does it do?
ContextLoaderListener is a class used to start up and shut down Spring’s root WebApplicationContext. It will create a web application context based on the “contextClass” and “contextConfigLocation” servlet context-params.
The purpose of the ContextLoaderListener is two-fold:
1) to tie the lifecycle of the ApplicationContext to the lifecycle of the ServletContext
2) to automate the creation of the ApplicationContext, so you don’t have to write explicit code to do create it - it’s a convenience function.
What are you going to do in the web.xml. Where do you place it?
The web.xml file is located under the webapp\WEB-INF dirrectory.
Inside web.xml, in order to load spring web application context we should:
* define ContextLoaderListener:
org. springframework.web.context.ContextLoaderListener * define WebApplicationContext configuration location via contextConfigLocation or contextClass settings: contextConfigLocation /WEB-INF/HelloWeb-servlet.xml
In order to provide Spring MVC functionality:
* define DispatcherServlet
myservlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation app-servlet.xml
- define DispatcherServlet mappingsmyservlet
*.jsp
How is an incoming request mapped to a controller and mapped to a method?
@RequestMapping annotation provides information regarding when the specific handler method should be called. it provides a way to specify the type of HTTP request on which the handler method should be called . The @RequestMapping can also be used at class level.
You can also declare the mapping to controller class (that implements a functional interface Controller and overrides handleRequest() method) by defining bean of type SimpleUrlHandlerMapping and setting the “mappings” property:
What is the @RequestParam used for?
@RequestParam is used on handler method parameter to map it with the request parameter coming with URL. It may have a name of URL parameter specified, but it may also be used without the name - then it is considered that handler method parameter name matches the URL parameter name.
What are the differences between @RequestParam and @PathVariable?
@PathVariable is used to map URI variables to handler method parameters and @RequestParam is used to map URL request parameters to handler method parameters.
What are some of the valid return types of a controller method?
A handler method typically returns a string value representing a logical view name. Controller methods can also return null or void, and in this case the default view is selected based on the request URL. Controller methods can also return concrete views, but thid is usually avoided because it couples the controller with the view implementation.
There are many return types are available for Handler method which is annotated by @RequestMapping inside controller like : ModelAndView (Class) Model (Interface) Map String void View HttpEntity> or ResponseEntity> HttpHeaders etc.
All handler methods must resolve to a logical view name that corresponds to a file, either explicitly by returning a String, View, or ModelAndView instance or implicitly based on internal conventions.
What is a View and what’s the idea behind supporting different types of View?
The View is responsible for rendering the model data and in general it generates HTML output that the client’s browser can interpret.
OOTB Spring supports JSP, Velocity templates, and XSLT views.
It also supports classes for creating PDFs, Excel spreadsheets,etc.
View interface prepares the request and forwards it to a view technology.
How is the right View chosen when it comes to the rendering phase?
The DispatcherServlet delegates to a ViewResolver to obtain View implementation based on view name.
What is the Model?
Java-5-specific interface that defines a holder for model attributes. Primarily designed for adding attributes to the model. Allows for accessing the overall model as a java.util.Map.