弹簧靴胸腺不加载html

发布于 2025-01-21 12:30:28 字数 993 浏览 1 评论 0原文

我有一个Java和Maven项目,其中我有以下控制器(src/main/java/com/example/romannumerals/controller/romannumeralController):

@RestController
public class RomanNumeralController {
    @GetMapping("/roman-numerals/{number}")
    public String RomanNumeral(@PathVariable int number){
        return RomanNumeralsService.numberToNumeral(number);
    }

    @GetMapping("/roman-numerals")
    public String RomanNumeral() {
        return "roman-numeral";
    }
}

i SOME HTML(src/main/resource/resources/resources/spemplates/roman-numeral.html)看起来像这样:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>From Numbers to Roman Numerals</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Roman Numerals
</body>
</html>

我希望当我在没有参数的控制器上调用该方法时会显示此HTML。取而代之的是,我将字符串“罗马 - 名字”印在屏幕上。我做错了吗?我以为百里叶会认识到我想显示带有该名称的HTML页面,而不是显示的字符串。

I have a Java and Maven project in which I have the following controller (src/main/java/com/example/romannumerals/controller/RomanNumeralController):

@RestController
public class RomanNumeralController {
    @GetMapping("/roman-numerals/{number}")
    public String RomanNumeral(@PathVariable int number){
        return RomanNumeralsService.numberToNumeral(number);
    }

    @GetMapping("/roman-numerals")
    public String RomanNumeral() {
        return "roman-numeral";
    }
}

I some HTML (src/main/resources/templates/roman-numeral.html) that looks like this:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>From Numbers to Roman Numerals</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Roman Numerals
</body>
</html>

I expected this HTML to be displayed when I called the method on the controller that has no parameter. Instead, I get the string "roman-numeral" printed to the screen. Am I doing something wrong? I thought Thymeleaf would recognise that I want to display the HTML page with that name instead of the string being displayed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2025-01-28 12:30:28

用@controller替换@RestController。 @RestController将使返回的对象成为HTTP响应主体。

Replace @RestController with @Controller. @RestController will make the returned object the HTTP response body.

旧城空念 2025-01-28 12:30:28

将@restController更改为@controller为我工作!

Changing @RestController to @Controller worked for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文