Spring MVC and the Web Layer Flashcards

1
Q

MVC is an abbreviation for a design pattern. What does it stand for and what is the idea
behind it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Do you need spring-mvc.jar in your classpath or is it part of spring-core?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the DispatcherServlet and what is it used for?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is the DispatcherServlet instantiated via an application context?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the root application context? How is it loaded?

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the @Controller annotation used for? How can you create a controller without an
annotation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the ContextLoaderListener and what does it do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are you going to do in the web.xml. Where do you place it?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is an incoming request mapped to a controller and mapped to a method?

A

@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:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the @RequestParam used for?

A

@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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the differences between @RequestParam and @PathVariable?

A

@PathVariable is used to map URI variables to handler method parameters and @RequestParam is used to map URL request parameters to handler method parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some of the valid return types of a controller method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a View and what’s the idea behind supporting different types of View?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is the right View chosen when it comes to the rendering phase?

A

The DispatcherServlet delegates to a ViewResolver to obtain View implementation based on view name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Model?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why do you have access to the model in your View? Where does it come from?

A

The DispatcherServlet passes the model data to the view.

17
Q

What is the purpose of the session scope?

A

When the bean is declared with @Scope(“session”), the spring IoC creates a bean instance for each HTTP session.

18
Q

What is the default scope in the web context?

A

Singleton - everywhere.

19
Q

Why are controllers testable artifacts?

A

Controller classes instances can be tested easily using MockMVC. It allows you to make calls of the required type to the controller and assert the response and other parameters.

20
Q

What does the InternalResourceViewResolver do?

A

InternalResourceViewResolver allows you to resolve views (usually JSP) that are internal resources to the application.
InternalResourceViewResolver id a default Spring view resolver. The default implementation can be overriden by registering a ViewResolver bean within the DispatcherServletContext.
The default view resolver implementation is also customizable and can be configured to look for view template files in a different location.