Skip to content

Commit c891f77

Browse files
author
litongjava
committed
add template engine
1 parent 08eb50e commit c891f77

File tree

7 files changed

+113
-101
lines changed

7 files changed

+113
-101
lines changed

docs/.vuepress/config/sidebar-zh.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
{
7272
"title": "07_aop",
7373
"collapsable": false,
74-
"children": ["07_aop/01.md", "07_aop/02.md", "07_aop/03.md", "07_aop/03.md", "07_aop/04.md"]
74+
"children": ["07_aop/01.md", "07_aop/02.md", "07_aop/03.md", "07_aop/03.md", "07_aop/04.md", "07_aop/05.md"]
7575
},
7676
{
7777
"title": "08_token",
@@ -134,9 +134,9 @@
134134
]
135135
},
136136
{
137-
"title": "16_常用注解",
137+
"title": "16_模版引擎",
138138
"collapsable": false,
139-
"children": ["16_常用注解/01.md"]
139+
"children": ["16_模版引擎/01.md"]
140140
},
141141
{
142142
"title": "17_mybatis",

docs/zh/06_内置组件/06.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 独立启动 tcp 服务器
1+
# 使用 tio-core 添加独立 tcp 端口的 处理 tcp 请求
22

33
使用 tio-boot 内置 tio-core,可以可以 tcp-core 启动 tcp 服务,处理 tcp 数据, tio-boot 提供了两种处理 tcp 数据数据的方式
44

docs/zh/06_内置组件/07.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# tio-boot 内置 tcp 处理器
1+
# 使用 tio-boot 内置 tcp 处理器 处理 tcp 请求
22

3-
tio-boot 内置了 tcp 的支持,可以使用一个端口支持 tcp,http,websocket 三种协议,当一个数据包发送到 tio-boot-server 时,TioBootServerHandler 会根据内置的判断方法,选择对应处理器进行协议的处理.
3+
tio-boot 内置了 tcp 的支持,运行在默认端口上,tio-boot 内置了协议识别算法,可以使用一个端口同时支持 tcp,http,websocket 三种协议,当一个数据包发送到 tio-boot-server 时,TioBootServerHandler 会根据内置协议识别算法,选择对应处理器进行协议的处理.
4+
5+
- tcp 协议 自定义的 Handler
6+
- websocket 协议 WebSocket 处理器
7+
- http 协议 Http 处理器
48

59
下面介绍如何使用 tio-boot 内置 Tcp 功能
610

docs/zh/06_内置组件/08.md

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

docs/zh/06_内置组件/09.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tio
1+
# Tio 工具类
22

33
`Tio`是一个用于管理网络通信的核心类,特别是在处理客户端和服务器之间的连接、消息发送、连接绑定和关闭等功能。类包含了大量的用于管理网络连接、发送和接收数据包、处理连接状态、以及管理连接的黑名单等操作的方法。这些方法使得开发者可以在客户端和服务器之间有效地进行通信,并对连接进行精细化管理.
44

docs/zh/16_常用注解/01.md docs/zh/07_aop/05.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# @AImport
22

3+
@AImport 用于导入其它工程中的配置到本工程中
4+
35
## 使用@AImport 注解整合 paddle-ocr-server
46

57
paddle-ocr-server 是笔者开发的款 ocr 识别应用,[开源地址](https://github.com/litongjava/ai-server/tree/main/paddle-ocr),paddle-ocr-server 完全基于 tio-boot 开发,所以可以非常方便的整合到 tio-boot 应用中
@@ -58,4 +60,4 @@ public class HelloApp {
5860
}
5961
```
6062

61-
然后运行项目访问 http://localhost/paddle/ocr/test 即可看到测试结果
63+
然后运行项目访问 http://localhost/paddle/ocr/test 即可看到测试结果

docs/zh/16_模版引擎/01.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)