epicUsername
epicUsername

Reputation: 1009

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

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

The example works without an error, but I am not able to wrap my head around why the addAttribute will not display the changed message, but rather returns only "message" as a string.

UPDATE The use of the @Controller annotation leads to an inability to execute for the project, and as a resolution, it needs to be @RestController. I'm not sure if that bears any significance for the general issue.

This is the controller code:

    package com.example.MySecondSpringBootProject.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HomeController {

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

This is the message html page:

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Demo Project</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html> 

And this is the output from "/message" on the locahost: 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!

Upvotes: 1

Views: 2405

Answers (2)

akram seid
akram seid

Reputation: 111

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

Upvotes: 1

el idrissi oussama
el idrissi oussama

Reputation: 46

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

Upvotes: 1

Related Questions