xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
appServlet
org.springframework.web.servlet.DispatcherServlet
/WEB-INF/spring/appServlet/servlet-context.xml
1
InterviewBitServlet
/
Here, the load-on-startup tag is 1 which indicates that the DispatcherServlet is instantiated whenever the Spring MVC application to the servlet container. During this process, it looks for the servlet-name-context.xml file and initializes beans that are defined in the file.
Question - 96 : - How is the root application context in Spring MVC loaded?
Answer - 96 : -
The root application context is loaded using the ContextLoaderListener that belongs to the entire application. Spring MVC allows instantiating multiple DispatcherServlet and each of them have multiple contexts specific to them. They can have the same root context too.
Question - 97 : - Where does the access to the model from the view come from?
Answer - 97 : -
The view requires access to the model to render the output as the model contains the required data meant for rendering. The model is associated with the controller that processes the client requests and finally encapsulates the response into the Model object.
Question - 98 : - Why do we need BindingResults?
Answer - 98 : -
BindingResults is an important Spring interface that is within the org.Springframework.validation package. This interface has a very simple and easy process of invocation and plays a vital role in detecting errors in the submitted forms. However, care has to be taken by the developer to use the BindingResult parameter just after the object that needs validation. For example:
@PostMapping("/interviewbit")
public String registerCourse(@Valid RegisterUser registerUser,
BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "home";
}
model.addAttribute("message", "Valid inputs");
return "home";
}
The Spring will understand to find the corresponding validators by checking the @Valid annotation on the parameter.
Question - 99 : - What are Spring Interceptors?
Answer - 99 : -
Spring Interceptors are used to pre-handle and post-handle the web requests in Spring MVC which are handled by Spring Controllers. This can be achieved by the HandlerInterceptor interface. These handlers are used for manipulating the model attributes that are passed to the controllers or the views.
The Spring handler interceptor can be registered for specific URL mappings so that it can intercept only those requests. The custom handler interceptor must implement the HandlerInterceptor interface that has 3 callback methods that can be implemented:
- preHandle()
- postHandle()
- afterCompletion()
The only problem with this interface is that all the methods of this interface need to be implemented irrespective of its requirements. This can be avoided if our handler class extends the HandlerInterceptorAdapter class that internally implements the HandlerInterceptor interface and provides default blank implementations.
Question - 100 : - Is there any need to keepspring-mvc.jar on the classpath or is it already present as part of spring-core?
Answer - 100 : -
The spring-mv.jar does not belong to the spring-core. This means that the jar has to be included in the project’s classpath if we have to use the Spring MVC framework in our project. For Java applications, the spring-mvc.jar is placed inside /WEB-INF/lib folder.