Doubt about the Controller in spring mvc

Asked

Viewed 49 times

1

Hello. I am studying the spring mvc and I am a few questions about its functioning. One of them is on the part of the controller.

When I fill out a form and request a controller method, are all the parameters I receive within that method in the request? In this example below the task and result parameters are inside the request and when the method is finished they will be passed to the view (and if there is an error in the task validation errors will be saved in result and returned to the form)?

@RequestMapping("adicionaTarefa")
public String adiciona(@Valid Tarefa tarefa, BindingResult result) {

    if(result.hasFieldErrors("descricao")) {
        return "tarefa/formulario";
    }   
    dao.adiciona(tarefa);
    return "tarefa/adicionada";
}

Thank you!

1 answer

1


We will answer in parts

When I fill out a form and request a controller method, all the parameters I receive within that method are in the request?

Yes, form data is in the body of your request.

The Bindingresult is evaluated in the validation phase, so it is not passed in the request, but included before the request is passed to your @Controller through the framework.

In this example below the task and result parameters are inside the request and when the method is finished they will be passed to the view (and if there is error in the task validation the errors will be saved in result and returned to the form)?

The things that are going to the screen can be found inside the object Model, which can also be included as a parameter for your view. The BindingResult will contain the validation errors about your object passed in the request, this information will then be passed to the view informed through the Model.

You can check what is being returned to the screen using the method Model.asMap() to verify what is being sent to the screen.

  • Thank you for clarifying my doubts, nullptr. Your explanation was very good.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.