gpt4 book ai didi

Thymeleaf 未加载模板

转载 作者:行者123 更新时间:2023-12-05 05:49:41 35 4
gpt4 key购买 nike

我正在使用 Spring 和 Thymeleaf。感谢 xerx593,我能够让它工作,所以我更新了这个问题以显示工作代码。

这是我的应用类

package com.propfinancing.www;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;

@Controller
@SpringBootApplication
public class PfWebApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(PfWebApplication.class, args);
}

@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

@GetMapping("/page1.html")
public String page1() {
return "page1";
}
}

接下来,我在 src/main/resources/templates/layout.html 中创建一个 layout.html 文件

<!DOCTYPE html>
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<body>
This is the layout template
<div layout:fragment="content">
<p>This is were the content will go</p>
</div>
</body>

</html>

还有鳍盟友,我创建了/ser/main/resources/templates/page1.html 来使用模板:

<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout}">

<body>
<div layout:fragment="content">
This is the content of page 1.
</div>
</body>

</html>

当我转到 http://dev.propfinancing.com/www/page1.html 时,它给了我预期的模板驱动输出。

谢谢!尼尔

最佳答案

最明显的错误:

  1. >

    I created a simple html page called page1.html in my src/main/resources/static directory

    (这对于(spring-web)静态内容来说非常棒,但是......)

  2. >

    And finally, I updated my page1.html to use the template...

更新还不够,您还必须移动它到配置的模板位置!因此,将文件移动到 src/main/resources/templates/(默认位置,发出 相同 浏览器请求, ) 将有望/可能产生所需的结果(,或至少抛出异常)。


简而言之:src/main/resources/static 目录不适用于模板! (它仍然可以配置,但这会很奇怪/hacky/一堆充满“副作用”!?)。


好的,404,可以(简单地)修复:

@Controller // !
@SpringBootApplication
public class ThymeleafTestApplication {

public static void main(String[] args) {
SpringApplication.run(ThymeleafTestApplication.class, args);
}

@GetMapping("/page1.html") // !!
public String page1() {
return "page1"; // returning the view name
}
// ...
}

即通过为此“ View ”提供“ Controller ”。

或者通过配置:

@SpringBootApplication
public class PfWebApplication // extends ...
implements WebMvcConfigurer {

@Override
public void addViewControllers (ViewControllerRegistry registry) {
ViewControllerRegistration r = registry.addViewController("/page1.html");
r.setViewName("page1");
// r.set...
}
...

一件重要的事情是:

  @Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

是“自动配置”方法,它为我们配备了“所有 Spring (引导)魔法”。

鉴于:

  @Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}

..是“DIY”方法,我们必须进行调整(例如 spring-boot does)。

链接:

关于Thymeleaf 未加载模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70634152/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com