|
| 1 | +# Enjoy 模版引擎 |
| 2 | + |
| 3 | +## 简介 |
| 4 | + |
| 5 | +tio-boot 内置 Enjoy 模版引擎,使用 Enjoy 模版引擎可以非常方便的返回网页内容 |
| 6 | + |
| 7 | +### 使用 |
| 8 | + |
| 9 | +### 返回模版 |
| 10 | + |
| 11 | +配置 EnjoyEngine |
| 12 | + |
| 13 | +``` |
| 14 | +
|
| 15 | +package com.litongjava.ai.chat.config; |
| 16 | +
|
| 17 | +import com.jfinal.template.Engine; |
| 18 | +import com.litongjava.jfinal.aop.annotation.Bean; |
| 19 | +import com.litongjava.jfinal.aop.annotation.Configuration; |
| 20 | +
|
| 21 | +@AConfiguration |
| 22 | +public class EnjoyEngineConfig { |
| 23 | +
|
| 24 | +private final String RESOURCE_BASE_PATH = "/templates/"; |
| 25 | +
|
| 26 | + @ABean |
| 27 | + public Engine engine() { |
| 28 | + Engine engine = Engine.use(); |
| 29 | + engine.setBaseTemplatePath(RESOURCE_BASE_PATH); |
| 30 | + engine.setToClassPathSourceFactory(); |
| 31 | + // 支持模板热加载,绝大多数生产环境下也建议配置成 true,除非是极端高性能的场景 |
| 32 | + engine.setDevMode(true); |
| 33 | + // 配置极速模式,性能提升 13% |
| 34 | + Engine.setFastMode(true); |
| 35 | + // jfinal 4.9.02 新增配置:支持中文表达式、中文变量名、中文方法名、中文模板函数名 |
| 36 | + Engine.setChineseExpression(true); |
| 37 | + return engine; |
| 38 | + } |
| 39 | +
|
| 40 | +} |
| 41 | +
|
| 42 | +``` |
| 43 | + |
| 44 | +在 Controoler 使用 Engine 获取网页并返回 |
| 45 | + |
| 46 | +``` |
| 47 | +
|
| 48 | +package com.litongjava.ai.chat.AController; |
| 49 | +
|
| 50 | +import com.jfinal.template.Engine; |
| 51 | +import com.jfinal.template.Template; |
| 52 | +import com.litongjava.jfinal.aop.Aop; |
| 53 | +import com.litongjava.tio.http.common.HttpRequest; |
| 54 | +import com.litongjava.tio.http.common.HttpResponse; |
| 55 | +import com.litongjava.tio.http.server.annotation.RequestPath; |
| 56 | +import com.litongjava.tio.http.server.util.Resps; |
| 57 | +@AController |
| 58 | +@RequestPath() |
| 59 | +public class TemplatesController { |
| 60 | + private Engine engine = Aop.get(Engine.class); |
| 61 | +
|
| 62 | + @RequestPath("/404") |
| 63 | + public Template notFound(HttpRequest request) { |
| 64 | + String fileName = "/404.html"; |
| 65 | + Template template = engine.getTemplate(fileName); |
| 66 | + return template; |
| 67 | + } |
| 68 | +
|
| 69 | + @RequestPath("/chat") |
| 70 | + public HttpResponse chat(HttpRequest request) { |
| 71 | + String fileName = "/chat.html"; |
| 72 | + return renderHtml(request, fileName); |
| 73 | + } |
| 74 | +
|
| 75 | + private HttpResponse renderHtml(HttpRequest request, String fileName) { |
| 76 | + Template template = engine.getTemplate(fileName); |
| 77 | + String string = template.renderToString(); |
| 78 | + HttpResponse html = Resps.html(request, string); |
| 79 | + return html; |
| 80 | + } |
| 81 | +} |
| 82 | +
|
| 83 | +``` |
| 84 | + |
| 85 | +解释一下上面的代码 |
| 86 | + |
| 87 | +这段代码包含两个类,配置了 JFinal 的 Enjoy 模板引擎,并在控制器中使用了该引擎。 |
| 88 | + |
| 89 | +1. `EnjoyEngineConfig` 类: |
| 90 | + |
| 91 | + - 使用 `@AConfiguration` 注解标记,表示这是一个配置类。 |
| 92 | + - `engine` 方法创建并配置 `Engine` 实例。设置了基础模板路径、类路径源工厂、开发模式、极速模式和支持中文表达式的选项。 |
| 93 | + |
| 94 | +2. `TemplatesController` 类: |
| 95 | + - 使用 `@RequestPath` 注解,无指定路径,表明它是一个处理 HTTP 请求的控制器。 |
| 96 | + - `notFound` 方法通过 Enjoy 模板引擎处理 `/404` 请求,返回 `/404.html` 模板。 |
| 97 | + - `chat` 方法处理 `/chat` 请求,返回 `/chat.html` 模板。 |
| 98 | + |
| 99 | +3.HandlerDispatcher.afterExecuteAction(HttpRequest, HttpResponse, Object) 方法中会 Template 类进行渲染并返回,可以在 action 使用 request.setAttribute("key", "value");设置参数到模版中 |
0 commit comments