简体   繁体   中英

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: 在此处输入图像描述

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM