You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.3生产环境JTE预编译模板找不到,报资源不存在

问题诊断与修复方案

1. 模板名称不匹配

控制器返回的"index.html"和预编译模板的引用规则冲突:你的模板文件是index.jte(对应配置gg.jte.templateSuffix=.jte),预编译后模板的引用名称应为不带后缀和.html的index,而非index.html。

修改控制器代码:

@Controller
public class IndexController {
    @GetMapping("/")
    public String indexAction() {
        return "index"; // 去掉.html,匹配预编译模板类名
    }
}

2. 自定义TemplateEngine缺失类加载器

你创建预编译模板引擎时未传入当前上下文的类加载器,导致无法找到打包在Jar中的预编译模板类。

修改TemplateConfiguration:

import gg.jte.ContentType;
import gg.jte.TemplateEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("prod")
class TemplateConfiguration {
    @Bean
    public TemplateEngine templateEngine() {
        // 传入当前类加载器,确保能加载Jar内的预编译模板类
        return TemplateEngine.createPrecompiled(getClass().getClassLoader(), ContentType.Html);
    }
}

3. 生产环境配置冗余

预编译模式下不需要读取模板文件,直接加载编译后的类即可,因此application-prod.properties中需删除gg.jte.templateLocation配置:

gg.jte.usePrecompiledTemplates=true
gg.jte.developmentMode=false
# 移除 gg.jte.templateLocation=src/main/resources/templates

4. 打包逻辑优化

原配置中通过fileTree("jte-classes")引入预编译类不够可靠,直接绑定precompileJte任务的输出目录更稳妥:

tasks.jar {
    dependsOn(tasks.precompileJte)
    from(tasks.precompileJte.get().outputDirectory) {
        include("**/*.class")
    }
}

5. 预编译结果验证

执行./gradlew precompileJte后,检查jte-classes目录下是否生成对应模板的类文件(比如com/playpen/templates/index.class,路径根据你的包结构和模板位置生成)。如果没有生成,确认jte.sourceDirectory指向的src/main/resources/templates目录下确实存在index.jte文件。


验证流程

  1. 执行./gradlew clean build生成生产包
  2. 启动服务:java -jar build/libs/xxx-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
  3. 访问根路径,确认模板正常渲染

内容的提问来源于stack exchange,提问作者nicholasnet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:43:10