Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Spring MVC interview questions and answers?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the Spring MVC interview questions and answers". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. MVC is an abbreviation for design pattern. What does it stand for? What's the idea behind it?

MVC is an acronym for Model-View-Controller design pattern. The model is based on the design principle of separation of concerns, which promotes the treatment of different functions of different layers and loose coupling between layers. In MVC mode, Model contains data that View and Controler help render in request processing and routing.

2. Do you need to use spring-mvc.jar in the classpath, or is it part of spring-core?

Spring-mvc.jar is not part of spring-core, and if you want to use the Spring MVC framework in your Java project, you must include spring-mvc.jar in the classpath of your application. In Java Web applications, spring-mvc.jar is usually placed in the / WEB-INF / lib folder.

3. What is DispatcherServlet and what is it used for?

DispatcherServlet is an implementation of the Front Controller design pattern that handles all incoming Web requests to Spring MVC applications. The front-end controller pattern is a common pattern in Web applications where all requests are received and routed to different components of the application for actual processing.

In the case of Spring MVC, DispatcherServlet routes the Web request to the Spring MVC controller.

In Spring MVC, DispatcherServlet is used to find the correct Controler to process the request, which is executed with the help of the handler map, such as the @ RequestMapping annotation.

It is also responsible for delegating the logical view name to the ViewResolver and then sending the rendered response to the client.

4. How can DispatcherServlet be instantiated through the application context?

DispatcherServlet is instantiated by a Servlet container such as Tomcat or Jetty. DispatcherServlet must be defined in the web.xml file.

You can see that load-on-startup is marked as 1, and DispatcherServlet is instantiated when the Spring MVC application is deployed to Tomcat or any other Servlet container. During instantiation, it looks for the file servlet-name-context.xml and initializes the bean defined in this file.

5. What is the root application context in Spring MVC? How did you load it?

In Spring MVC, the context loaded with ContextLoaderListener is called the "root" application context, which belongs to the entire application, while the application context initialized with DispatcherServlet is actually specific to that servlet.

Technically, Spring MVC allows multiple DispatcherServlet to be used in Spring MVC Web applications, so there may be multiple such contexts, each for the corresponding servlet, but with the same root context.

6. What is the purpose of @ Controller annotations? How do I create a controller without comments?

@ Controller is the Spring MVC annotation used to define Controller, but it's really just a prototype annotation. You can even create a controller without @ Controller by annotating the Spring MVC controller class with @ Component. The actual work of mapping requests to handler methods is done using the @ RequestMapping annotation.

7. What is ContextLoaderListener and what is its function?

ContextLoaderListener is a listener, which helps boot Spring MVC. As its name implies, it loads and creates ApplicationContext, so you don't have to write explicit code to create it. The application context is where Spring bean leaves. For Web applications, there is a subclass called WebAppliationContext.

ContextLoaderListener also links the life cycle of ApplicationContext to the life cycle of ServletContext. You can use the getServletContext () method to get the ServletContext from WebApplicationContext.

8. How are incoming requests mapped to controllers and methods?

Sometimes I ask this question: how does DispatcherServlet know which Controller should handle the request?

Spring uses handler mappings to associate controllers with requests, and the two common handler mappings are BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping.

In BeanNameUrlHandlerMapping, when the request url matches the name of the bean, the class in the bean definition is the controller that will process the request.

On the other hand, in SimpleUrlHandlerMapping, the mapping is clearer. The number of URL can be specified, and each URL can be explicitly associated with the controller.

By the way, if you use annotations to configure Spring MVC, you should use the @ RequestMapping annotation to map incoming requests to controller and handler methods.

You can also configure the @ RequestMapping annotation through the URI path, query parameters, the HTTP method of the request, and the HTTP header that exists in the request.

9. What is @ RequestParam for?

RequestParam is an Spring MVC comment that is used to extract request or query parameters from the handler method of the controller in URL

The @ RequestParam annotation also supports data type conversion. For example, you can see here that a String is automatically converted to long, but it can also cause an exception. If the query parameter does not exist or the type does not match, you can also use requried = false to make the parameter optional, such as @ RequestParam (value = "id", required = false).

10. What is a model?

Model is again a reference that encapsulates data or output for rendering. Always create the model and pass it to the view in Spring MVC. If the mapped controller method takes Model as the method parameter, the Spring framework automatically injects the model instance into the method. Any properties set on the injection model are retained and passed to View.

11. Why can the controller test the workpiece?

In Spring, MVC controllers are testable artifacts because they are not directly integrated with any View technology. They only return a logical view name and can be easily tested.

12. What are the components of spring mvc?

(1) pre-controller DispatcherServlet.

(2) Mapping controller HandlerMapping.

(3) processor Controller.

(4) Model and view ModelAndView.

(5) View parser ViewResolver.

12. The difference between @ Autowired and @ Resource?

@ Autowired performs bean matching in byType mode by default, and @ Resource matches bean in byName mode by default.

@ Autowired is the annotation of Spring, and @ Resource is the annotation of J2EE. The package names of these two annotations are clear when you take a look at the imported annotations.

Spring belongs to a third party, and J2EE is Java's own thing, so it is recommended to use @ Resource annotations to reduce the coupling between code and Spring.

13. What is Spring's MVC framework?

Spring is equipped with a full-function MVC framework for building Web applications. Spring can be easily integrated with other MVC frameworks, such as Struts,Spring 's MVC framework, which clearly separates business objects from control logic with control reversals. It also allows you to bind request parameters to business objects declaratively.

14. During the rendering phase, how to choose the correct view?

The view is selected by ViewResolver in Spring MVC. When Controller returns the logical view name to DispatcherServlet, it queries ViewResolver to find the correct View. ViewResolver parses logical views into physical resources, such as JSP pages or FreeMarker templates, based on its implementation.

For example, InternalResourceViewResolver is a default ViewResolver for converting logical view names, such as using the prefix and suffix "hello" to "/ WEB-INF/hello.jsp".

This is the end of the content of "Spring MVC interview questions and answers". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report