4 Spring MVC
1. Spring MVC overview
1.1What is Spring MVC?
Framework for building web applications in Java
Based on Model-view-controller design pattern
Leverages features of the Core Spring Framework(Ioc, DI)
1.2 Spring MVC benefits
The Spring way of building web app UIs in Java
Leverage a set of reusable UI components
Help manage application state for web requests
Process form data: validation, conversion etc
Flexible configuration for the view layer
1.3 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)
1.4 Spring MVC Front Controller
Front controller known as DispatcherServlet
Part of the Spring Framework
Already developed by Spring Dev Team
You will create
Model objects(orange)
View templates(dark green)
Controller classes(yellow)
1.5 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
1.6 Model
model: contains your data(like a suitcase)
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
1.7 View Template
Spring MVC is flexible
Supports many view templates
Most common is JSP+JSTL(JSP standard tag library)
Developer creates a page
display data
2. Spring MVC Configuration
2.1 Spring MVC Config Process
Add configurations to file: WEB-INF/web.xml
Configure Spring MVC Dispatcher Servlet
Set up URL mappings to Spring MVC Dispatcher Servlet
Add configurations to file: WEB-INF/spring-mvc-demo-servlet.xml
Add support for Spring component scanning
Add support for conversion, formatting and validation
3. Spring MVC - Creating Controllers and Views
3.1 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
Step 3&4. Add Request Mapping to Controller method
4. Reading Form Data with Spring MVC
Development Process
Create controller class
Show HTML form: a. Create controller method to show HTML Form b. Create View Page for HTML form
Process HTML Form: a. Create controller method to process HTML Form b. Develop View Page for Confirmation
5. Adding Data to the Spring Model
Spring Model
The Model is a container for your application data
In your Controller
You can put anything in the Model
String, objects, info from database, etc...
Your View page(JSP) can access data from the model
e.g.
6. Request Params and Request Mappings
6.1 Binding Request Params
1. Bind variable using @RequestParam Annotation
e.g.
2. Add Controller @RequestMapping
Serves as parent mapping for controller
All request mappings on methods in the controller are relative
Similar to folder directory structures
7. Spring MVC Form Tags
Spring MVC Form Tags are the building block for a web page
Form Tags are configurable and reusable for a web page
7.1 Data Binding
Spring MVC Form Tags can make use of data binding
Automatically setting / retrieving data from a Java object / bean
7.2 Form tags will generate HTML for you:
7.3 How to reference Spring MVC Form Tags
Specify the Spring namespace at beginning of JSP file
7.3.1 Text Field
In the Spring Controller
Before you show the form, you must add a model attribute
This is a bean that will hold form data for the data binding
When Form is Loaded ... fields are populated. Spring MVC will call get method.
When Form is submitted... calls setter methods.
Last updated