Home > Mobile >  Spring Boot model.addAttribute - how to get message to display correctly?
Spring Boot model.addAttribute - how to get message to display correctly?

Time:11-14

I found the following example to walk myself through a part of SpringBoot: enter image description here

I've looked at various implementations of addAttribute here and they all use an incoming value from a frontend to assign a value to said attribute, which makes sense for this endpoint, but in this case, the frontend is pulling the value passed into it from the second parameter of the method - at least it should.

I've tried this, to no effect:

 @RequestMapping("/message")
public String message(Model model, @RequestParam(value="message") String message) {
    model.addAttribute("message", "This is a custom message");
    return "message";
}

Passing a second argument in the method, also to no effect, it just returns the "message" string:

@GetMapping("/message")
public String message(Model model, String str) {
    model.addAttribute("message", "This is a custom message");
    return "message";
}

I will keep looking into it, but my head is going into circles at this point, or I'm just not quite understanding something about the model.addAttribute concept.

Thank you in advance!

CodePudding user response:

try this solution :

@GetMapping("/message")
public String message(Model model) {
    String msg = "This is a custom message" ;
    model.addAttribute("msg", msg);
    return "message";
}

for the view :

<h2 th:text="${msg}" align="center"></h2>

i hope this help you

CodePudding user response:

You are returning the string "message" try returning return model.addAtribute("message","this is custom message");.

  • Related