0% found this document useful (0 votes)
5 views3 pages

Spring MVC

The document outlines various architectures in Spring MVC, including Model 1, Model 2, and Front Controller architecture, highlighting their differences in handling presentation and business logic. It explains key components such as DispatcherServlet, ViewResolver, and validation mechanisms, along with examples of controller methods and annotations used in Spring MVC. Additionally, it discusses the popularity of Spring MVC due to its adherence to MVC principles and extensive feature support.

Uploaded by

yadeedya katuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Spring MVC

The document outlines various architectures in Spring MVC, including Model 1, Model 2, and Front Controller architecture, highlighting their differences in handling presentation and business logic. It explains key components such as DispatcherServlet, ViewResolver, and validation mechanisms, along with examples of controller methods and annotations used in Spring MVC. Additionally, it discusses the popularity of Spring MVC due to its adherence to MVC principles and extensive feature support.

Uploaded by

yadeedya katuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. What is Model 1 architecture?

o Model 1 architecture uses JSP (or HTML) to handle both presentation and business
logic. It lacks a clear separation between concerns, making it harder to maintain.

2. What is Model 2 architecture?

o Model 2 is a variation of MVC (Model-View-Controller). It separates the presentation


(JSP), controller (Servlet), and business logic (Java Beans or services), making the
application easier to manage and scale.

3. What is Model 2 Front Controller architecture?

o This architecture uses a single controller (like DispatcherServlet) to handle all


requests, centralizing request processing and making it easier to apply common
functionality like logging, security, and authentication.

4. Can you show an example controller method in Spring MVC?

o @Controller

o public class HomeController {

o @GetMapping("/home")

o public String showHomePage(Model model) {

o model.addAttribute("message", "Welcome to Spring MVC!");

o return "home"; // returns home.jsp or home.html

o }

o }

5. Can you explain a simple flow in Spring MVC?

o Client sends a request.

o DispatcherServlet receives the request.

o It maps the request to a controller method.

o Controller returns a logical view name and model data.

o ViewResolver maps the logical view to an actual view file.

o View is rendered and response is returned to the client.

6. What is a ViewResolver?

o A ViewResolver maps a logical view name to the actual view (JSP, Thymeleaf, etc.).

7. What is Model?

o A container object in Spring MVC to pass data from controller to view.

8. What is ModelAndView?
o A holder for both Model and View in a single return value. It contains the data and
the view name.

9. What is a RequestMapping?

o An annotation used to map web requests to specific handler methods or classes in


Spring MVC.

10. What is Dispatcher Servlet?

o The central front controller in Spring MVC that handles all incoming requests and
routes them to the appropriate controller.

11. How do you set up Dispatcher Servlet?

o In Spring Boot, it is auto-configured.

o In traditional Spring MVC:

o <servlet>

o <servlet-name>dispatcher</servlet-name>

o <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

o </servlet>

o <servlet-mapping>

o <servlet-name>dispatcher</servlet-name>

o <url-pattern>/</url-pattern>

o </servlet-mapping>

12. What is a form backing object?

o An object used to hold form data and bind it to a Java object in Spring MVC.

13. How is validation done using Spring MVC?

o By using JSR-380/JSR-303 annotations like @NotNull, @Size, etc., along with @Valid
in controller methods.

14. What is BindingResult?

o An object that holds validation errors after binding request data to a model.

15. How do you map validation results to your view?

o Use BindingResult in controller method. If it has errors, return the form view with
error messages.

16. What are Spring Form Tags?

o JSP tags provided by Spring to bind form inputs to model attributes easily. (e.g.,
<form:input>, <form:errors>)

17. What is a Path Variable?


o A dynamic value in the URI mapped using @PathVariable. Example: /user/{id}.

18. What is a Model Attribute?

o An attribute added to the model and used to populate form fields or display data. It’s
also annotated using @ModelAttribute.

19. What is a Session Attribute?

o An attribute stored in the HTTP session using @SessionAttributes.

20. What is a init binder?

o A method annotated with @InitBinder that customizes data binding and format
conversions (like date formats).

21. How do you set default date format with Spring?

o @InitBinder

o public void initBinder(WebDataBinder binder) {

o SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

o binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, false));

o }

22. How do you implement common logic for controllers in Spring MVC?

o By using a base controller class or @ControllerAdvice.

23. What is a controller advice?

o A class annotated with @ControllerAdvice that contains global exception handlers,


model attributes, and data binders.

24. What is @ExceptionHandler?

o Used inside a controller or controller advice to handle specific exceptions.

25. Why is Spring MVC so popular?

o It follows MVC principles, supports loose coupling, integrates easily with view
technologies, and provides extensive features for form handling, validation,
exception handling, and REST API support.

You might also like