Spring Thymeleaf 无法解析模板

西蒙

我的问题是我无法显示 Thymeleaf 模板。我怀疑存在配置错误,但我似乎无法找到它。提前致谢 :)

pom.xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
...
<properties>
        ...
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.0.3</thymeleaf-layout-dialect.version>
    </properties>
...
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>

模板在: src/main/resources/templates/home.html

弹簧配置:

server.servlet.context-path=/mityo
server.port=8080
...
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.mode=html5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=/resources/templates/

控制器:

@Controller
@RequestMapping("buy")
public class HomeController {

    public String buy(Model model) {
        return "home";
    }
}

我正在尝试访问:localhost:8080/mityo/buy并获得 404。

另一件事,任何人都可以解释(或提供指向文档的链接)哪个 servlet 用于“返回”模板 html?它是 Spring 调度程序 servlet 吗?


好的,所以我忘记了@GetMapping,谢谢@benjamin c。但是添加它会产生:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "home", template might not exist or might not be accessible by any of the configured Template Resolvers
阿迪娜·罗利亚

请删除配置: spring.thymeleaf.prefix=/resources/templates/ 默认情况下,此位置是 Thymeleaf 引擎使用的位置。

更新:如果要更改默认位置(在资源目录下),可以使用: spring.thymeleaf.prefix=classpath:/templates/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章