Skip to content

greetingSubmit method is quite confusing #9

@buptfarmer

Description

@buptfarmer

In this project, the following code lead me to a great misunderstanding, I want to share it, and maybe I can help others.

// GreetingController.java
 @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }

// result.html
<p th:text="'content: ' + ${greeting.content}"/>

At first glance in the result.html, I thought greeting object may refer to the @ModelAttribute Greeting greeting, then I tried to make some modification to use another model OtherModel, but it turned out to be exception.
After google quite a while, I found out that here the ${greeting.content} is linked to the name of the model class rather than the name of the key.
So I believe that the proper way to do this is like this.

    @PostMapping("/greeting2")
    public String greetingSubmit(@ModelAttribute OtherModel greeting, Model model) {
        model.addAttribute("greeting", greeting);
        return "result";
    }

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions