2021-08-17
This commit is contained in:
18
thymeleaf/src/main/java/kr/pe/elex/examples/Person.java
Normal file
18
thymeleaf/src/main/java/kr/pe/elex/examples/Person.java
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
}
|
||||
91
thymeleaf/src/main/java/kr/pe/elex/examples/Thymeleaf.java
Normal file
91
thymeleaf/src/main/java/kr/pe/elex/examples/Thymeleaf.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.messageresolver.StandardMessageResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||
import org.thymeleaf.templateresource.ITemplateResource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@Slf4j
|
||||
public class Thymeleaf {
|
||||
private final TemplateEngine templateEngine;
|
||||
|
||||
@SneakyThrows
|
||||
public Thymeleaf() {
|
||||
templateEngine = new TemplateEngine();
|
||||
templateEngine.setTemplateResolver(new MyTemplateResolver());
|
||||
templateEngine.setMessageResolver(new MyMessageResolver());
|
||||
}
|
||||
|
||||
public String process(String template, Context context) throws IOException {
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
templateEngine.process(template, context, writer);
|
||||
|
||||
String out = writer.toString();
|
||||
writer.close();
|
||||
return out;
|
||||
}
|
||||
|
||||
private static class MyTemplateResolver extends ClassLoaderTemplateResolver {
|
||||
MyTemplateResolver() {
|
||||
super();
|
||||
this.setTemplateMode(TemplateMode.HTML);
|
||||
this.setSuffix(".html");
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyMessageResolver extends StandardMessageResolver {
|
||||
@SneakyThrows
|
||||
MyMessageResolver() {
|
||||
super();
|
||||
|
||||
this.setDefaultMessages(properties(getClass()
|
||||
.getResourceAsStream("/messages.properties")));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
protected Map<String, String> resolveMessagesForTemplate(String template, ITemplateResource templateResource, Locale locale) {
|
||||
|
||||
Map<String, String> map = propertiesToMap(getClass()
|
||||
.getResourceAsStream("/messages_" + locale.getLanguage() + ".properties"));
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, String> propertiesToMap(InputStream is) throws IOException {
|
||||
Properties properties = properties(is);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
for (String key : properties.stringPropertyNames()) {
|
||||
map.put(key, properties.getProperty(key));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static Properties properties(InputStream is) throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.load(new InputStreamReader(is, StandardCharsets.UTF_8));
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
9
thymeleaf/src/main/resources/messages.properties
Normal file
9
thymeleaf/src/main/resources/messages.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
# Examples for Java
|
||||
#
|
||||
# Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
# https://www.elex-project.com/
|
||||
#
|
||||
|
||||
hello = Hello
|
||||
only = Only in ROOT!
|
||||
8
thymeleaf/src/main/resources/messages_ko.properties
Normal file
8
thymeleaf/src/main/resources/messages_ko.properties
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# Examples for Java
|
||||
#
|
||||
# Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
# https://www.elex-project.com/
|
||||
#
|
||||
|
||||
hello = 안녕
|
||||
12
thymeleaf/src/main/resources/sample.html
Normal file
12
thymeleaf/src/main/resources/sample.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Thymeleaf Sample</title>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
</head>
|
||||
<body>
|
||||
<p><span th:text="#{hello}"></span><span th:text="${person.name}">NAME</span></p>
|
||||
<p th:text="#{only}"></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import com.elex_project.abraxas.Console;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.thymeleaf.context.Context;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
class ThymeleafTest {
|
||||
|
||||
@Test
|
||||
void test() throws IOException {
|
||||
Context context = new Context();
|
||||
///context.setLocale(Locale.ENGLISH);
|
||||
context.setVariable("person", new Person("Charlie", 14));
|
||||
|
||||
Thymeleaf thymeleaf = new Thymeleaf();
|
||||
String out = thymeleaf.process("sample.html", context);
|
||||
|
||||
Console.writeLine(out);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user