Spring Model View Controller (MVC)

33. What is Spring MVC framework? 
Spring Model View Controller (MVC) is the Web module of the Spring Framework. It provides a rich functionality for building robust Web Applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. 
The MVC framework of Spring is request-driven and is designed around a central servlet, calledDispatcherServlet that dispatches requests to controllers. 
Spring’s DispatcherServlet is completely integrated with Spring IoC container and allows you to use every other feature of Spring. 
34. What is DispatcherServlet? 
In Spring MVC, the client sends a request to the Web container in the form of HTTP request. This incoming request is intercepted by the Front controller which is the DispatcherServlet and it then tries to find out appropriate handler mappings. The DispatcherServlet dispatches the request to the appropriate controller with the help of handler mappings. Once controller returns the model along with the logical view, DispatcherServlet takes the help of ViewResolver to resolve the view and will pass the model data to the view, which is finally rendered on the browser. You can, therefore, consider DispatcherServlet as the entry point of Spring MVC is the. It is a normal servlet class which implements HttpServlet base class. 
35. How to upload a file in Spring MVC application? 
The consider MultipartResolver interface is used for uploading files in Spring MVC. The Spring Framework provides two types of considering MultipartResolver implementations. One is to use with Apache Commons FileUpload. The other for use with Servlet 3.0 multipart request parsing. 
36. What is the use of @RestController annotation? 
This annotation is used at the class level. The consider @RestController annotation marks the class as a REST controller where every method returns a domain object instead of a view. By annotating a class with this annotation you no longer need to add a response body to all the consider RequestMapping methods. It means that you no more use view-resolvers or send HTML in response. You just send the domain object as HTTP response in the standard format like JSON that is understood by the REST consumers. 
37. What are Spring profiles? 
Spring allows developers to register beans by condition. Using the @Profile annotation you can map the bean to a particular profile. The annotation simply takes the names of one (or multiple) profiles. You can now map your beans to different profiles – for example, dev, test, and prod. You can then activate different profiles in different environments to bootstrap just the beans you need. The @Profile annotation can be applied at the class level or method level. Any @Component or @Configurationcan be marked with @Profile to control when it is loaded. 
38. What are some of the important Spring MVC annotations you have used? 
Some important annotations in Spring MVC are: 
@Controller: Used on Java classes that play the role of controller in your application. The @Controllerannotation allows auto detection of component classes in the classpath and auto-registering bean definitions for them. To enable auto detection of such annotated controllers, you can add component scanning to your
configuration. The Java class annotated with @Controller is capable of handling multiple request mappings. 
@RequestMapping: Used in both class and method level. The @RequestMapping annotation is used to map web requests onto specific handler classes and handler methods. When @RequestMapping is used on a class level, it creates a base URI for which the controller will be used. When this annotation is used on methods, it will give you the URI on which the handler methods will be executed. From this, you can infer that the class level request mapping will remain the same whereas each handler method will have their own request mapping. Sometimes you may want to perform different operations based on the HTTP method used, even though the request URI may remain the same. In such situations, you can use @RequestMapping method attribute to narrow down the HTTP methods in order to invoke the methods of your class. 
@PathVariable: Used to annotate request handler method arguments. The @RequestMapping annotation can be used to handle dynamic changes in the URI where certain URI value acts as a parameter. The@PathVariable annotation can be used to declare this parameter. 
@RequestAttribute: Used to bind the request attribute to a handler method parameter. Spring retrieves the value of the named attribute to populate the parameter annotated with @RequestAttribute
@ResponseBody: Used to annotate request handler methods. The @ResponseBodyannotation is similar to the@RequestBody annotation. The @ResponseBodyannotation indicates that the result type should be written straight in the response body in a format you specify, such as JSON. Spring converts the returned object into a response body by using the HttpMessageConveter
39. What is ContextLoaderListener? 
Usually, when you build multi-tier applications you don’t want to clutter all the beans in one config file[appname]-servlet.xml. For example, consider you configure Spring Security and you want to include all those beans in a separate security-context.xml. Similarly, you want to configure all the beans belonging to service layer in applicationContext.xml, and beans belonging to DAO layer in dao-context.xml. When you configure all these beans in different context files, you need to somehow let know Spring that these files exist, because Spring only knows about [appname]- servlet.xml. It is the ContextLoaderListener that helps Spring recognize all the other context files. 
40. What is Spring Expression Language (SpEL)? 
The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. Basically, SpEL helps you in querying an object at runtime, instead of having to depend only on configuration files. You can use it for both XML and annotation based configurations. It supports a wide range of relational expressions to accessing properties of a class or method. 
41. Name some view technologies that Spring supports. 
Some view technologies that Spring supports are: 
Thymeleaf 
Groovy Markup Template Engine 
Velocity & FreeMarker 
JSP & JSTL 
Tiles

Comments

Popular posts from this blog

Spring Annotations

Spring Data Access