# JVue Template Engine SpringBoot Starter
**Repository Path**: iteachyou/jvue-template-engine-spring-boot-starter
## Basic Information
- **Project Name**: JVue Template Engine SpringBoot Starter
- **Description**: JVue Template Engine SpringBoot Starter 是 JVue Template Engine 的 Spring Boot 自动配置模块
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 10
- **Forks**: 0
- **Created**: 2026-04-24
- **Last Updated**: 2026-05-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: 模板引擎, jvue
## README
## JVue Template Engine - Spring Boot Starter
[](https://spring.io/projects/spring-boot)
[](https://openjdk.org/)
[](LICENSE)
[](https://gitee.com/iteachyou/jvue-template-engine-spring-boot-starter)
## 简介
`jvue-template-engine-spring-boot-starter` 是 [JVue Template Engine](https://gitee.com/iteachyou/jvue-template-engine) 的 **Spring Boot 自动配置模块**,提供:
- ✅ **零配置接入** — 引入依赖即自动装配,无需手动初始化 `JVueTemplateEngine`
- ✅ **视图解析器集成** — 无缝融入 Spring MVC 视图层,兼容 `Model` / `ModelAndView`
- ✅ **自动 Bean 扫描** — 自定义指令、方法通过 Spring Bean 自动注册
- ✅ **多源模板加载** — 同时支持 Classpath 和文件系统,按优先级 fallback
- ✅ **完美兼容 Spring Boot 3.x**(测试通过 **3.5.13**)
- ✅ **开发模式热重载**
---
## 快速开始
### 第一步:添加依赖
> 本 Starter 已传递依赖核心引擎,无需单独引入。
```xml
暂无待办事项
``` ### 第三步:编写 Controller ```java @Controller public class HelloController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("title", "我的待办"); model.addAttribute("greeting", "你好"); model.addAttribute("user", currentUser()); model.addAttribute("todoList", todoService.list()); return "hello"; // → 解析为 templates/hello.html } } ``` ### 第四步:启动项目 ``` . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.13) [JVue] Template engine initialized. prefix=classpath:/templates/ suffix=.html cache=true ``` 访问 `http://localhost:8080/hello` 即可看到渲染结果。 --- ## 配置项参考 在 `application.yml` 中配置: ```yaml jvue: template: prefix: classpath:/templates/ # 模板路径前缀 suffix: .html # 模板文件后缀名 cache: true # 是否启用缓存(生产环境建议开启) encoding: UTF-8 # 编码格式 ``` 对应 `application.properties`: ```properties jvue.template.prefix=classpath:/templates/ jvue.template.suffix=.html jvue.template.cache=true jvue.template.encoding=UTF-8 ``` --- ## 使用方式 ### 标准 MVC 模式 ```java @Controller @RequestMapping("/report") public class ReportController { @Autowired private ReportService reportService; @GetMapping("/{id}") public String detail(@PathVariable Long id, Model model) { model.addAttribute("report", reportService.getById(id)); return "report/detail"; // → templates/report/detail.html } } ``` ### ModelAndView 模式 ```java @GetMapping("/dashboard") public ModelAndView dashboard() { ModelAndView mav = new ModelAndView("dashboard/index"); mav.addObject("stats", statsService.summary()); return mav; } ``` ### 程序化调用(注入引擎) ```java @Autowired private JVueTemplateEngine jVueTemplateEngine; public String renderEmail(Order order) { return jVueTemplateEngine.render("email/order", Map.of("orderNo", order.getOrderNo(), "items", order.getItems())); } ``` ### REST API 返回 HTML ```java @RestController @RequestMapping("/api/preview") public class PreviewController { @Autowired private JVueTemplateEngine jVueEngine; @PostMapping public ResponseEntity{{ str.maskName('张三丰') }}
{{ math.round(3.14159, 2) }}
``` ### 静态方法注册(YAML 配置) ```yaml jvue: template: static-methods: - cc.iteachyou.jvue.demo.JVueStaticTemplateMethods#wrapTag - cc.iteachyou.jvue.demo.JVueStaticTemplateMethods#sumTag ``` --- ## 自动装配原理 ``` Spring Boot 启动 │ ├─ 扫描 META-INF/spring/AutoConfiguration.imports │ └─ JVueTemplateAutoConfiguration │ ├─ 读取 JVueTemplateProperties (jvue.template.*) │ ├─ 创建 JVueTemplateEngine Bean │ ├─ 扫描并自动注册: │ ├─ JVueDirectiveHandler → 自定义指令 │ ├─ @JVueMethod 标注的方法 → 自定义函数 │ └─ static-methods 配置 → 静态工具方法 │ └─ 注册 JVueTemplateViewResolver 到 Spring MVC (优先级高于 Thymeleaf / FreeMarker) ``` --- ## 版本兼容性 | Starter 版本 | JVue Engine | Spring Boot | JDK | |-------------|-------------|-------------|-----| | **1.0.2** | 1.0.2 | 3.5.x | 17+ | | 1.0.1 | 1.0.1 | 3.x | 17+ | > Spring Boot 3.x 要求 JDK 17+。 --- ## 常见问题 **Q:模板修改后不生效?** 开发环境设置 `jvue.template.cache=false` 关闭缓存即可。 **Q:如何在非 Web 场景使用?** 直接注入 `JVueTemplateEngine` Bean 调用 `render()` 方法,不依赖 HTTP 上下文。 **Q:能直接在表达式中调用 Spring Bean 吗?** 不能。需通过 `@JVueMethod` 注解或 `static-methods` 配置注册后使用。 **Q:和 Thymeleaf / FreeMarker 能共存吗?** 可以,但需注意视图解析器优先级。如需 JVue 作为默认,可调整 `order` 配置。 --- ## 相关项目 | 项目 | 说明 | |------|------| | [jvue-template-engine](https://gitee.com/iteachyou/jvue-template-engine) | 🔧 核心模板引擎 | | [jvue-template-engine-spring-boot-starter](https://gitee.com/iteachyou/jvue-template-engine-spring-boot-starter) | 🚀 本项目 · Spring Boot 自动配置 | | [jvue-template-engine-spring-boot-starter-demo](https://gitee.com/iteachyou/jvue-template-engine-spring-boot-starter-demo) | 🎮 完整功能演示项目 | --- ## 参与贡献 欢迎提交 Issue 和 Pull Request! 1. Fork 本仓库 2. 创建功能分支:`git checkout -b feat/my-feature` 3. 提交修改:`git commit -m 'feat: add my feature'` 4. 推送分支:`git push origin feat/my-feature` 5. 提交 Pull Request --- ## 开源协议 本项目基于 [MIT](LICENSE) 协议开源。 ---