Skip to content

Commit

Permalink
bugfix number variable in freemarker template display error (#1334)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Nov 14, 2023
1 parent dca7c62 commit 6191ecb
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 106 deletions.
7 changes: 1 addition & 6 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- freemaker依赖 -->
<!-- freemaker 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.entity.manager.NoticeTemplate;
import org.dromara.hertzbeat.common.support.event.SystemConfigChangeEvent;
import org.dromara.hertzbeat.common.util.JsonUtil;
import org.dromara.hertzbeat.common.util.ResourceBundleUtil;
import org.dromara.hertzbeat.manager.component.alerter.AlertNotifyHandler;
import org.dromara.hertzbeat.manager.service.NoticeConfigService;
Expand All @@ -52,6 +51,7 @@
@Slf4j
abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {

private static final String NUMBER_FORMAT = "0";
protected static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
protected ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");
@Resource
Expand All @@ -63,10 +63,10 @@ abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {


protected String renderContent(NoticeTemplate noticeTemplate, Alert alert) throws TemplateException, IOException {

StringTemplateLoader stringLoader = new StringTemplateLoader();
freemarker.template.Template templateRes;
Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
cfg.setNumberFormat(NUMBER_FORMAT);
Map<String, Object> model = new HashMap<>(16);
model.put("title", bundle.getString("alerter.notify.title"));

Expand Down Expand Up @@ -109,6 +109,7 @@ protected String renderContent(NoticeTemplate noticeTemplate, Alert alert) throw
log.error("alert does not have mapping default notice template. type: {}.", type());
throw new NullPointerException(type() + " does not have mapping default notice template");
}
// TODO 单实例复用缓存 考虑多线程问题
String templateName = "freeMakerTemplate";
stringLoader.putTemplate(templateName, noticeTemplate.getContent());
cfg.setTemplateLoader(stringLoader);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String buildAlertHtmlTemplate(final Alert alert, NoticeTemplate noticeTem
}
monitorId = monitorId == null ? "External Alarm, No ID" : monitorId;
monitorName = monitorName == null ? "External Alarm, No Name" : monitorName;
// Introduce thymeleaf context parameters to render pages
// Introduce context parameters to render pages
Map<String, String> model = new HashMap<>(16);
model.put("nameTitle", bundle.getString("alerter.notify.title"));
model.put("nameMonitorId", bundle.getString("alerter.notify.monitorId"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public boolean sendTestMsg(NoticeReceiver noticeReceiver) {
tags.put(CommonConstants.TAG_THRESHOLD_ID, "200");
Alert alert = new Alert();
alert.setTags(tags);
alert.setId(100L);
alert.setId(1003445L);
alert.setTarget(ALERT_TEST_TARGET);
alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL);
alert.setContent(ALERT_TEST_CONTENT);
Expand Down
11 changes: 3 additions & 8 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ spring:
# need to disable spring boot mongodb auto config, or default mongodb connection tried and failed..
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML
template-resolver-order: 1
freemarker:
enabled: false

management:
health:
Expand Down Expand Up @@ -172,4 +167,4 @@ alerter:
scheduler:
server:
enabled: true
port: 1158
port: 1158
4 changes: 2 additions & 2 deletions manager/src/main/resources/templates/1-EmailTemplate.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="en">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Expand Down Expand Up @@ -871,4 +871,4 @@
</tbody>
</table>
</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.thymeleaf.TemplateEngine;

import java.io.IOException;
import java.util.ResourceBundle;
Expand All @@ -32,9 +31,6 @@ class MailServiceTest {
@InjectMocks
private MailServiceImpl mailService;

@Mock
private TemplateEngine templateEngine;

@Mock
private AlerterProperties alerterProperties;

Expand Down
11 changes: 5 additions & 6 deletions script/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ spring:
static-locations:
- classpath:/dist/
- classpath:../dist/
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML
# need to disable spring boot mongodb auto config, or default mongodb connection tried and failed..
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
freemarker:
enabled: false

management:
health:
Expand Down
11 changes: 5 additions & 6 deletions script/docker-compose/hertzbeat-mysql-iotdb/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ spring:
static-locations:
- classpath:/dist/
- classpath:../dist/
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML
# need to disable spring boot mongodb auto config, or default mongodb connection tried and failed..
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
freemarker:
enabled: false

management:
health:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ spring:
static-locations:
- classpath:/dist/
- classpath:../dist/
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML
# need to disable spring boot mongodb auto config, or default mongodb connection tried and failed..
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
freemarker:
enabled: false

management:
health:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ spring:
static-locations:
- classpath:/dist/
- classpath:../dist/
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML
# need to disable spring boot mongodb auto config, or default mongodb connection tried and failed..
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
freemarker:
enabled: false

management:
health:
Expand Down
10 changes: 2 additions & 8 deletions script/helm/hertzbeat/templates/manager/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ data:
# need to disable spring boot mongodb auto config, or default mongodb connection tried and failed..
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML
template-resolver-order: 1
freemarker:
enabled: false
management:
health:
mail:
Expand Down

0 comments on commit 6191ecb

Please sign in to comment.