liveBook · Manning

Application model

As explained in Spring in Action, Sixth Edition

Model is a Spring MVC object that carries attributes from a controller to a view.

An application model is a Spring MVC object that carries attributes from a controller to a view. A controller populates the Model, then returns a logical view name; the view uses those attributes to render a response. section 2.1.2

Supplying a design form

For a GET request to /design, showDesignForm() adds an empty Taco to the Model under the key taco and returns the logical view name taco. The view receives the blank Taco and can render a form for creating it. section 2.1.2

Listing 2.4 The beginnings of a Spring controller class
package tacos.web;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
 
import lombok.extern.slf4j.Slf4j;
import tacos.Ingredient;
import tacos.Ingredient.Type;
import tacos.Taco;
import tacos.TacoOrder
 
@Slf4j
@Controller
@RequestMapping("/design")
@SessionAttributes("tacoOrder")
public class DesignTacoController {
 
@ModelAttribute
public void addIngredientsToModel(Model model) {
    List<Ingredient> ingredients = Arrays.asList(
      new Ingredient("FLTO", "Flour Tortilla", Type.WRAP),
      new Ingredient("COTO", "Corn Tortilla", Type.WRAP),
      new Ingredient("GRBF", "Ground Beef", Type.PROTEIN),
      new Ingredient("CARN", "Carnitas", Type.PROTEIN),
      new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES),
      new Ingredient("LETC", "Lettuce", Type.VEGGIES),
      new Ingredient("CHED", "Cheddar", Type.CHEESE),
      new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
      new Ingredient("SLSA", "Salsa", Type.SAUCE),
      new Ingredient("SRCR", "Sour Cream", Type.SAUCE)
    );
 
    Type[] types = Ingredient.Type.values();
    for (Type type : types) {
      model.addAttribute(type.toString().toLowerCase(),
          filterByType(ingredients, type));
    }
  }
 
  @ModelAttribute(name = "tacoOrder")
  public TacoOrder order() {
    return new TacoOrder();
  }
  
  @ModelAttribute(name = "taco")
  public Taco taco() {
    return new Taco();
  }
 
  @GetMapping
  public String showDesignForm() {
    return "design";
  }
 
  private Iterable<Ingredient> filterByType(
      List<Ingredient> ingredients, Type type) {
    return ingredients
              .stream()
              .filter(x -> x.getType().equals(type))
              .collect(Collectors.toList());
  }
 
}

Inspect the controller listing to see Model used alongside @Controller, @RequestMapping, @GetMapping, and @ModelAttribute. section 2.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains population with an object and rendering through a logical view name.
Examples
Uses a /design request, an empty Taco attribute, the taco key, and the taco logical view.
What you can do after
Apply it via Spring MVC to pass form data from a controller to a rendered view.
How it connects
Connects Model with controllers, views, HTTP GET requests, and @ModelAttribute.

Also explained in

Compare how 2 books explain application model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.