'sprinboot: should i use a bean for an object representing a form?

I'm a newbie when it comes to spring boot and currently i'm unsure what the best approach is when creating a form page. Do I,

a. use a make the object represting modelAttribute of the form a bean and inject it?

b. instantiate the object inside the controller itself?

which is the best option and why?

option a:

@Controller
@RequestMapping("/game")
public class GameController {
    private IGameDao gameDao;
    private TicketOrderForm ticketOrderForm;
    private TicketFormValidator ticketFormValidator;

    @Autowired
    public void setTicketFormValidator(TicketFormValidator ticketFormValidator) {
        this.ticketFormValidator = ticketFormValidator;
    }

    @Autowired
    public void setGameDao(IGameDao gameDao) {
        this.gameDao = gameDao;
    }


    @Autowired
    public void setTicketOrder(TicketOrderForm ticketOrderForm) {
        this.ticketOrderForm = ticketOrderForm;
    }

    @PostMapping
    public String getGameOverview(@ModelAttribute StadiumCommand stadiumCommand, Model model) {
        model.addAttribute("stadium", stadiumCommand.getStadiumSelected());
        model.addAttribute("games", gameDao.getGamesByStadium(stadiumCommand.getStadiumSelected()));
        return "game/game-overview";
    }

    @GetMapping("/{id}")
    public String getGameById(@PathVariable Long id, Model model) {
        // set default values
        ticketOrderForm.setTickets(1);
        ticketOrderForm.setSoccerCode1(10);
        ticketOrderForm.setSoccerCode2(25);
        ticketOrderForm.setGameId(id);

        model.addAttribute("ticketOrderForm", ticketOrderForm);
        model.addAttribute("game", gameDao.get(id));
        model.addAttribute("gameId", id);

        return "game/game-ticket-form";
    }

    @PostMapping("/ticket")
    public String onSubmit(
            @Valid @ModelAttribute TicketOrderForm ticketOrder,
            BindingResult result,
            Model model
    ) {
        ticketFormValidator.validate(ticketOrder, result);
        if(result.hasErrors()){
            return "game/game-ticket-form";
        }
        return "home-page";
    }
}

option b:

@Controller
@RequestMapping("/game")
public class GameController {
    private IGameDao gameDao;
    private TicketOrderForm ticketOrderForm;
    private TicketFormValidator ticketFormValidator;

    @Autowired
    public void setTicketFormValidator(TicketFormValidator ticketFormValidator) {
        this.ticketFormValidator = ticketFormValidator;
    }

    @Autowired
    public void setGameDao(IGameDao gameDao) {
        this.gameDao = gameDao;
    }


    @Autowired
    public void setTicketOrder(TicketOrderForm ticketOrderForm) {
        this.ticketOrderForm = ticketOrderForm;
    }

    @PostMapping
    public String getGameOverview(@ModelAttribute StadiumCommand stadiumCommand, Model model) {
        model.addAttribute("stadium", stadiumCommand.getStadiumSelected());
        model.addAttribute("games", gameDao.getGamesByStadium(stadiumCommand.getStadiumSelected()));
        return "game/game-overview";
    }

    @GetMapping("/{id}")
    public String getGameById(@PathVariable Long id, Model model) {
        TicketOrderForm form = new TicketOrderForm();
        form.setTickets(1);
        form.setSoccerCode1(10);
        form.setSoccerCode2(25);
        form.setGameId(id);

        model.addAttribute("ticketOrderForm", ticketOrderForm);
        model.addAttribute("game", gameDao.get(id));
        model.addAttribute("gameId", id);

        return "game/game-ticket-form";
    }

    @PostMapping("/ticket")
    public String onSubmit(
            @Valid @ModelAttribute TicketOrderForm ticketOrder,
            BindingResult result,
            Model model
    ) {
        ticketFormValidator.validate(ticketOrder, result);
        if(result.hasErrors()){
            return "game/game-ticket-form";
        }
        return "home-page";
    }
}

the correspon


<form:form method="POST" action="ticket" modelAttribute="ticketOrderForm">
    <form:errors path="email" cssClass="error"/>
    <div class="mb-3"><label for="email" class="form-label">email:</label>
      <form:input cssClass="form-control" path="email" id="email" size = "20"/>&nbsp;
      <form:errors path="email" cssClass="error"/>
    </div>
    <div class="mb-3"><label for="tickets" class="form-label">tickets:</label>
      <form:input cssClass="form-control" path="tickets" id="tickets" size = "20"/>&nbsp;
      <form:errors path="tickets" cssClass="error"/>
    </div>
    <div class="mb-3"><label for="soccerCode1" class="form-label">soccerCode1:</label>
      <form:input cssClass="form-control" path="soccerCode1"  id="soccerCode1" size = "20"/>&nbsp;
      <form:errors path="soccerCode1" cssClass="error"/>
    </div>
    <div class="mb-3"><label for="soccerCode2" class="form-label">soccerCode2:</label>
      <form:input cssClass="form-control" path="soccerCode2" id="soccerCode2" size = "20"/>&nbsp;
      <form:errors path="soccerCode2" cssClass="error"/>
    </div>
    <input class="btn btn-primary" type="submit" value="OK" />
    <%@include file="../component/csfr.jsp"%>
  </form:form>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source