Spring MVC Flashcards

1
Q

Explain MVC

A

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.

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

Which pattern does Spring MVC implement which listens for all the web requests and then forwards it to respective controllers?

A

FrontController. All requests first lant on this servlet which then forwards it to proper controller based on url mapping.

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

What does controller return and who processes that information.

A

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.

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

What is the name of FrontController in spring?

A

DispatcherServlet.

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

Which class creates WebApplication context, etc?

A

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

How is xml file for web application context located?

A

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

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

Which are mandatory configurations in web.xml for spring?

A

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.

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

Role of controllers in spring?

A

execute the actual request, prepare the model and select a view to render. Controller is the glue that glues logic and web interface.

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

Which annotation is used to desginate a class as controller

A

@Controller is used. Don’t forget to enable component scan in xml.

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

Which configuration is required for enabling of creation of controller and other stereotypes using annotation?

A

context:component-scan - property base package = “co.edureka.package”
So all the classes inside that package are scanned.

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

Which annotation is required to declare that a method will handle requests for a particular endpoint?

A

@RequestMapping is used. Need to provide endpoint in it.

@RequestMapping(value=”/home”, method=POST)

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

What are the objects that hold form values called?

A

Command objects. In form tag you need to configure command name which is the name of model.

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

How to do data binding in spring?

A

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.

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

Which jar contains Spring form tag library

A

spring-webmvc.jar contains it.

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

Sample spring form configuration

A

form:form action=”register” method=”post” commandName=”userForm”
form:input path=”username”
form:password path=”password”
input type=”submit” value=”Register”

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

Sample controller that handles user form with command name of “userForm”

A

@RequestMapping(method=RequestMethod.POST)
public String registerUser(@ModelAttribute(“userForm”) User user, Map model) {
….
}

17
Q

Form validation in Spring using javax.validation?

A
User {
   @NotNull
   @Size(min=4, max=15)
   private String username;
   @Pattern(emailPattern)
   private String email;
}

@RequestMapping(“/signup”, RequestMethod.POST)
public String signup(Model model, @Valid User user, BindingResult result) {
if (result.hasErrors()) {
….
}
}

18
Q

To use Internationalization which extra beans need to be configured?

A

LocalChangeInterceptor - You can enable changing locale by adding it to one of handler mappings. It will detect parameter in request and change the locale.
SessionLocaleResolver - Allows you to retrieve locale and timezone from session that might be associated with user’s request
ResourceBundleMessageSource - resolves text messages from properties file based on selected locales.

If localeResolver is not added, then deafult of AcceptHeaderLocaleResolver is used. It uses accept-language in HTTP request.

19
Q

Which tag is used for internationalization support of showing locale specific text?

A

spring:message tag is used
code property is used to denote key in properties file
text property is the default value if key is not found in file

20
Q

Which class is used to upload file in Spring?

A

Spring provides CommonsMultipartResolver bean to handle file upload and multi part file that represents file uploaded by user.
It is defined in Jakarta commons FileUpload.

21
Q

Which class represents file contents uploaded by user?

A

MultipartFile repsents the multi part file uploaded by user. The contents can be stored in memory or temporarily on disk. It is the responsibility of user to store the contents in persistent storage.

22
Q

How to upload file from spring jsp?

A

form:form metho=”POST” commandName=”ResumeUploadForm” encType=”multipart/form-data”
input type=”file” name=”file”
input type=”submit” value=”Upload”

23
Q

Where should one keep static files in Spring MVC application?

A

Best practice is to keep in one parent directory like static and create seperate directories for images, javascript, css etc.

24
Q

Can one directly access static files from jsp?

A

No. We have to configure static files path in spring mvc file. We must use mvc:resources tag and provide path to static files and give mapping.
so lets say path is /resources/ and mapping is /staticContent/**. Then you can use /staticContent/css/main.css in your jsp.

25
Q

How to show form errors on spring form?

A

with each form element you can have form:errors tag.

tr
td
form:input path=”username”
form:errors path=”username”

26
Q

How can one handle exception thrown from controller in spring?

A

@ExceptionHandler annotation can be used to annotate a method which will handle that exception.

@ExceptionHandler(ClassNotFoundException.class)
public String handleClassNotFound(Exception ex) {
…. do logic
return “logicalViewName”;
}

27
Q

How can one handle exception thrown from any controller globally?

A

@ControllerAdvice anotation can be used to annotate a class. And then this class becomes global exception handler for all controllers.

28
Q

What is shortcoming of @ExceptionHandler anotation?

A

It only works for current controller so one will have to repeat exception handling logic in all controllers. To avoid that use @ControllerAdvice to define a class that handles exceptions on global level for resuability.

29
Q

What is apache tiles?

A

Apache tiles is template framework to simplify development of web applications. We can define page fragments which are then combined at runtime. Like header, footer, maincontent, left panel, right panel etc.
These templates are useful for consistent look and feel across application.

30
Q

How to integrate tiles in spring? Which configurations are required in spring mvc xml

A

TilesViewResolver bean
TilesConfigurer bean and it has property “definitions” which is list of tiles layout definitions.

/WEB-INF/layouts/default.xml
/WEB-INF/layouts/index.xml

31
Q

What is configured in tiles definition file?

A

tiles-definition
definition name=”home” template=”/WEB-INF/views/template/SiteTemplate.jsp”
put-attribute name=”title” value=”Home”
put-attribute name=”header” value=”/WEB-INF/views/template/header.jsp”

div …
img …