MVC basic Flashcards
How Spring MVC Works behind the scenes
Components of a Spring MVC Application
- A set of web pages to layout UI components
- A collection of Spring beans (controllers, services, etc…)
- Spring configuration (XML, Annotations or Java)
Spring MVC Front Controller
Spring MVC Front Controller
Front controller known as DispatcherServlet Part of the Spring Framework
Already developed by Spring Dev Team
You as a developer will create
Model objects (orange)
View templates (dark green)
Controller classes (yellow)
Spring MVC Front Controller
What is a Controller?
- Code created by developer
- Contains your business logic
- Handle the request
- Store/retrieve data (db, web service…)
- Place data in model
- Send to appropriate view template
Model
- Model: contains your data
- Store/retrieve data via backend systems
- database, web service, etc…
- Use a Spring bean if you like
- Place your data in the model
- Data can be any Java object/collection
View Template
- Spring MVC is flexible
- Supports many view templates
- Most common is JSP + JSTL
- Developer creates a page
- Displays data
Spring MVC Configuration Process - Part 1
Add configurations to file: WEB-INF/web.xml
- Configure Spring MVC Dispatcher Servlet
- Set up URL mappings to Spring MVC Dispatcher Servlet
Spring MVC Configuration Process - Part 2
Add configurations to file: WEB-INF/spring-mvc-demo-servlet.xml
- Add support for Spring component scanning
- Add support for conversion, formatting and validation
- Configure Spring MVC View Resolve
Spring MVC example
Development Process
- Create Controller class
- Define Controller method
- Add Request Mapping to Controller method
- Return View Name
- Develop View Page
Step 1: Create Controller class
Annotate class with @Controller
- @Controller inherits from @Component … supports scanning
@Controller
public class HomeController { }
Step 2: Define Controller method
@Controller public class HomeController {
public String showMyPage() { …
}
}
Step 3: Add Request Mapping to Controller method
@Controller public class HomeController {
@RequestMapping(“/”) public String showMyPage() {
return “main-menu”; }
}
Find View Page Magic