TCS Logo

Asked in TCS and 11 others

What is the architecture of Spring MVC?

AnswerBot
1mo

Spring MVC is a framework for building web applications in Java, following the Model-View-Controller design pattern.

  • 1. Model: Represents the data and business logic. Example: A User class that holds u...read more

Anonymous
1y
Spring MVC (Model-View-Controller) is a framework within the Spring Framework for building web applications. It follows the MVC design pattern, which separates the application into three interconnected components: Model, View, and Controller. This separation helps manage the complexity of large applications and promotes a clean separation of concerns. ### Components of Spring MVC: 1. **Model**: - **Role**: Represents the data and business logic of the application. - **Components**: Includes objects that hold data (often called domain objects or entities) and services that perform business operations. - **Interaction**: The controller updates the model based on user input, and the view reads from the model to display data. 2. **View**: - **Role**: Responsible for rendering the user interface, displaying data from the model to the user. - **Components**: JSP, Thymeleaf, or any other template engine. The view technologies can generate HTML, JSON, XML, etc. - **Interaction**: The controller returns the view name, and the view resolver maps this name to an actual view. 3. **Controller**: - **Role**: Handles user input and interactions, processes requests, updates the model, and returns a view to be rendered. - **Components**: Typically Java classes annotated with `@Controller`. - **Interaction**: It processes incoming web requests, delegates operations to the service layer, updates the model, and returns the view name. ### How Spring MVC Works: 1. **Request Handling**: - A user sends a request to the web server. - The DispatcherServlet, which is the front controller in Spring MVC, intercepts the request. 2. **DispatcherServlet**: - It is configured in the `web.xml` (or as a Spring Boot configuration). - It dispatches the request to the appropriate controller based on the request URL and configured mappings. 3. **Controller**: - The appropriate controller handles the request. The controller method is usually annotated with `@RequestMapping` or more specific annotations like `@GetMapping` and `@PostMapping`. - The controller processes the request, interacts with the service layer to perform business operations, and updates the model. 4. **Model and View**: - The controller returns a `ModelAndView` object, which includes the model data and the view name. - The view resolver maps the view name to the actual view, like a JSP or Thymeleaf template. 5. **View Rendering**: - The view engine renders the view by combining the view template with the model data. - The rendered view is sent back to the user as the response. ### Example: Here's a simple example to illustrate the flow: #### Controller: ```java @Controller public class HomeController { @GetMapping("/home") public String home(Model model) { model.addAttribute("message", "Welcome to Spring MVC!"); return "home"; // View name } } ``` #### View (home.html with Thymeleaf): ```html " style="color:#5670FB;text-decoration:underline;font-family:Figtree-Regular"> http://www.thymeleaf.org"> Home

Default Message

``` ### Explanation: 1. **Request**: A user navigates to ` http://localhost:8080/home`. 2. **DispatcherServlet**: Intercepts the request and finds the appropriate controller. 3. **Controller**: The `home` method of `HomeController` handles the request. It adds a message to the model and returns the view name "home". 4. **View Resolver**: Resolves the view name "home" to `home.html`. 5. **View**: The view engine (Thymeleaf in this case) renders `home.html`, replacing `${message}` with "Welcome to Spring MVC!". 6. **Response**: The rendered HTML is sent back to the user's browser. ### Summary: Spring MVC's use of the MVC design pattern promotes a clean separation of concerns, making web application development more modular and manageable. The framework provides robust infrastructure and tools to handle web requests, process business logic, manage data, and render views effectively.
Manisha .Chintapalli
2y
A java flow which is used to build web applications
MANDAR SHAHANE
2y

A Spring MVC is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inve...read more

Select
Add answer anonymously...

Interview Questions from Popular Companies

Accenture Logo
3.7
 • 8.7k Interviews
Amazon Logo
4.0
 • 5.4k Interviews
HCLTech Logo
3.5
 • 4.2k Interviews
Infosys Logo
3.6
 • 8k Interviews
Cognizant Logo
3.7
 • 6k Interviews
Wipro Logo
3.7
 • 6.2k Interviews
Tech Mahindra Logo
3.5
 • 4.2k Interviews
Capgemini Logo
3.7
 • 5.1k Interviews
Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits