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”
Sample controller that handles user form with command name of “userForm”
@RequestMapping(method=RequestMethod.POST)
public String registerUser(@ModelAttribute(“userForm”) User user, Map model) {
….
}
Form validation in Spring using javax.validation?
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()) {
….
}
}
To use Internationalization which extra beans need to be configured?
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.
Which tag is used for internationalization support of showing locale specific text?
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
Which class is used to upload file in Spring?
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.
Which class represents file contents uploaded by user?
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.
How to upload file from spring jsp?
form:form metho=”POST” commandName=”ResumeUploadForm” encType=”multipart/form-data”
input type=”file” name=”file”
input type=”submit” value=”Upload”
Where should one keep static files in Spring MVC application?
Best practice is to keep in one parent directory like static and create seperate directories for images, javascript, css etc.
Can one directly access static files from jsp?
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.