15 Must-Know Spring MVC Interview Questions
15 Must-Know Spring MVC Interview Questions
Questions
Further, you can talk about your experience with Spring, if any. That’ll add a lot of weight
to your answer.
6. What are some benefits of Spring MVC framework over other MVC
frameworks?
The Spring MVC framework has some clear benefits over other frameworks. Some of
the benefits are:
1. Clear separation of roles – There is a specialised object for every role, thus
providing a clear separation of roles.
2. Reusable business code – With Spring MVC, you don’t need to duplicate your
code. You can use your existing objects as commands instead of mirroring them
in order to extend a particular framework base class.
3. Customizable binding and validation
4. Customizable locale and theme resolution
5. Customizable handler mapping and view resolution
6. From Spring 2.0 onwards, the framework comes with a JSP form tag library
which makes writing forms in JSP pages much easier.
7. What is DispatcherServlet?
Spring MVC framework is request-driven and is designed around a central Servlet that
handles all the HTTP requests and responses. The DispatcherServlet, however, does a
lot more than just that. It seamlessly integrates with the IoC container and allows you to
use each feature of Spring.
11. A user gets a validation error in other fields on checking a checkbox, after
which, he unchecks it. What would be the current selection status in command
object of the Spring MVC? How will you fix this issue?
This is one of the trickier questions to answer if you aren’t aware of the HTTP Post
behaviour in Spring MVC.
During HTTP Post, if you uncheck the checkbox, then HTTP does not include a request
parameter for the checkbox - which means the updated selection won’t be picked up. To
fix that, you can use hidden form field which starts with ‘_’.
12. How will you compare the MVC framework to the three-tier architecture?
A Three-tier architecture is an architecture style whereas MVC is a design pattern.
Having said that, in larger applications, MVC forms the presentation tier of a three-tier
architecture. The Model, View, and Controller are concerned only with the presentation -
they use the middle tier to populate their models.
13. How should we use JDBC in Spring to optimise the performance?
Spring provides a template class called as JDBCTemplate. Using JDBC with this
template gives manifolds better performance.
The Spring framework is extendable, that is, you can create your own scope as well.
The “scope” attribute of the bean element is used to define the scope.
Wrapping Up…
@ComponentScan: It is used when we want to scan a package for beans.
@Service: It is also used at class level. It tells the Spring that class contains the
business logic.
@PostMapping: It maps the HTTP POST requests on the specific handler method.
singleton scope. singleton is default bean scope in spring container. It tells the
container to create and manage only one instance of bean class, per container. This
single instance is stored in a cache of such singleton beans, and all subsequent
requests and references for that named bean return the cached instance.
Spring Spring Boot
4. To test the Spring project, we need to set up the Spring Boot offers
server explicitly. embedded servers such as
Jetty and Tomcat, etc.
@PathVariable
@PathVariable is a Spring annotation which indicates that a method parameter
should be bound to a URI template variable.
It has the following optional elements:
package com.zetcode.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping(path="/{name}/{age}")
public String getMessage(@PathVariable("name") String name,
@PathVariable("age") String age) {
return msg;
}
}
@RequestParam
@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam String id) {
return "ID: " + id;
}
we used @RequestParam to extract the id of query parameter.
http://localhost:8080/spring-mvc-basics/api/foos?id=abc
----
ID: abc