简体   繁体   English

Spring Boot model.addAttribute - 如何让消息正确显示?

[英]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我找到了以下示例来引导自己完成 SpringBoot 的一部分: 在此处找到

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.该示例可以正常运行,但我无法理解为什么 addAttribute 不会显示已更改的消息,而是仅将“消息”作为字符串返回。

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 . UPDATE @Controller注解的使用导致项目无法执行,作为解决方案,需要@RestController I'm not sure if that bears any significance for the general issue.我不确定这对一般问题是否有任何意义。

This is the controller code:这是 controller 代码:

    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:这是消息html页面:

    <!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:这是本地主机上“/message”的 output: 在此处输入图像描述

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.我在这里查看了 addAttribute 的各种实现,它们都使用来自前端的传入值来为所述属性分配一个值,这对该端点有意义,但在这种情况下,前端正在从中提取传递给它的值该方法的第二个参数 - 至少应该如此。

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.我会继续研究它,但此时我的脑袋开始转圈,或者我只是不太了解 model.addAttribute 概念。

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");您正在返回字符串“message”尝试返回return model.addAtribute("message","this is custom message"); . .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 model.addAttribute()参数 - model.addAttribute() parameters 在Spring MVC中-model.addAttribute用于动态数据以及如何将数据放置在jsp文件中 - In Spring MVC - model.addAttribute usage for dynamic data and how to place the data in jsp file 春天的@ModelAttribute,model.addAttribute有什么区别? - What is the difference between @ModelAttribute, model.addAttribute in spring? model.addAttribute() 对于每个循环 - model.addAttribute() For Each Loop spring mvc use model.addAttribute(nav)无法使用$ {nav.id}获取jsp中父类的属性 - spring mvc use model.addAttribute(nav) can not get the parent class’s attributes in jsp with ${nav.id} Model.addAttribute 不向 .jsp 传递任何东西 - Model.addAttribute not passing any thing to the .jsp Spring MVC:在Controller中使用model.addAttribute()将各种列表发送到Ajax方法 - Spring MVC : Using model.addAttribute() in Controller to send various Lists to Ajax method frontpage从model.addAttribute获取对象的长类型ID失去精度 - frontpage get object's long type id from model.addAttribute lose precision 如何使用 Model.addAttribute() 添加的变量作为参数传递给 thymeleaf 的 hasRole 方法? - How can I use the variables that added by Model.addAttribute() to pass as a parameter in hasRole method of thymeleaf? 做“model.addAttribute()”和“session.setAttribute()”的区别 - difference in doing “model.addAttribute()” and “session.setAttribute()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM