4 Spring MVC
Last updated
Was this helpful?
Last updated
Was this helpful?
Framework for building web applications in Java
Based on Model-view-controller design pattern
Leverages features of the Core Spring Framework(Ioc, DI)
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
A set of web pages to layout UI components
A collection of Spring beans(controllers, services, etc...)
Spring configuration(XML, Annotations or Java)
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)
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: 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
Spring MVC is flexible
Supports many view templates
Most common is JSP+JSTL(JSP standard tag library)
Developer creates a page
display data
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
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
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
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.
e.g.
Serves as parent mapping for controller
All request mappings on methods in the controller are relative
Similar to folder directory structures
Spring MVC Form Tags are the building block for a web page
Form Tags are configurable and reusable for a web page
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:
Form Tag
Description
form: form
main form container
form:input
text field
form:textarea
multi-line text field
form:checkbox
check box
form:radiobutton
radio buttons
form:select
drop down list
more...
Specify the Spring namespace at beginning of JSP file
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.
Configure Spring MVC view Resolver