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

  1. Configure Spring MVC Dispatcher Servlet

  2. Set up URL mappings to Spring MVC Dispatcher Servlet

Add configurations to file: WEB-INF/spring-mvc-demo-servlet.xml

  1. Add support for Spring component scanning

  2. Add support for conversion, formatting and validation

3. Spring MVC - Creating Controllers and Views

3.1 Development Process

  1. Create Controller class

  2. Define Controller method

  3. Add Request Mapping to Controller method

  4. Return View Name

  5. 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

@Controller
public class HomeController{

    @RequestMapping("/")
    public String showMyPage(){
        return "main-menu";//step 4: view name
    }

}

4. Reading Form Data with Spring MVC

Development Process

  1. Create controller class

  2. Show HTML form: a. Create controller method to show HTML Form b. Create View Page for HTML form

  3. 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.

    //new a controller method to read form data and add data to the model
    @RequestMapping("/processFormVersionTwo")
    public String letsShoutDude(HttpServletRequest request, Model model){
        //read the request parameter from the html form
        String theName = request.getParameter("studentName");
        //convert the data to all caps
        theName = theName.toUpperCase();
        //create the message
        String result = "Yo!+  "+theName;
        //add message to the model
        model.addAttribute("message", result);
        return "helloworld";
    }

6. Request Params and Request Mappings

6.1 Binding Request Params

1. Bind variable using @RequestParam Annotation

e.g.

public String processFormVersionThree(@RequestParam("studentName") String theName, Model model){
 ...
}

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

<%@ taglib prefix = "form" uri="http://www.springframework.org/tags/form"  %>

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