thymeleafの初心者です。
A.htmlの中にB.htmlのbodyをインクルードしたいです。
B.htmlは素のhtmlじゃなきゃいけないので、fragmentは使えません。
直接B.htmlと書く場合はうまく行きますが、
例えばサーバ側(Model)からhtmlFilenameという変数が渡されて、
それをB.htmlの部分に置き換えたい場合は、
どう書けば良いでしょうか。
ではうまく行きませんでした。
よろしくお願いいたします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/03/02 08:28

回答1件
0
ベストアンサー
とりあえず、Thymeleaf 単体で動かして見ます。
参考: Thymeleafテンプレートエンジンをそれ単体で使う
Thymeleaf : thymeleaf-3.1.1.RELEASE (今の最新は 3.1.2 みたいですがたまたまテスト環境に入ってたのでそのまま)
java
1import java.io.FileWriter; 2import java.io.Writer; 3 4import org.thymeleaf.TemplateEngine; 5import org.thymeleaf.context.Context; 6import org.thymeleaf.context.IContext; 7import org.thymeleaf.templateresolver.FileTemplateResolver; 8 9public class Main { 10 public static void main(String[] args) throws Exception { 11 TemplateEngine engine = initializeTemplateEngine(); 12 IContext context = makeContext("B.html", "foo", "bar", "baz"); 13 try(Writer writer = new FileWriter("output.html")) { 14 engine.process("input", context, writer); 15 } 16 } 17 18 private static TemplateEngine initializeTemplateEngine() { 19 TemplateEngine templateEngine = new TemplateEngine(); 20 FileTemplateResolver resolver = new FileTemplateResolver(); 21 resolver.setTemplateMode("XHTML"); 22 resolver.setPrefix(""); 23 resolver.setSuffix(".html"); 24 templateEngine.setTemplateResolver(resolver); 25 return templateEngine; 26 } 27 28 private static IContext makeContext(String htmlFilename, String... args) { 29 Context context = new Context(); 30 context.setVariable("htmlFilename", htmlFilename); 31 context.setVariable("args", args); 32 return context; 33 } 34}
input.html
html
1<?xml version="1.0" encoding="utf-8"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" 4 xmlns:th="http://www.thymeleaf.org" xml:lang="ja" lang="ja"> 5<head> 6<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 7<title>Sample</title> 8</head> 9<body th:replace="${htmlFilename}::body"> 10 <h1>Arguments</h1> 11 <p> 12 The sample application received 13 <span th:text="${#arrays.isEmpty(args)} ? 'no' : 'some'">...</span> 14 arguments. 15 </p> 16 <ul> 17 <li th:each="a : ${args}"><span th:text="${a}">...</span></li> 18 </ul> 19</body> 20</html>
B.html
html
1<?xml version="1.0" encoding="utf-8"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xml:lang="ja" lang="ja"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6<title>B</title> 7</head> 8<body> 9 <p>BBBBBBBBBBBBB</p> 10</body> 11</html>
実行結果 output.html
html
1<?xml version="1.0" encoding="utf-8"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" 4 xml:lang="ja" lang="ja"> 5<head> 6<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 7<title>Sample</title> 8</head> 9<body> 10 <p>BBBBBBBBBBBBB</p> 11</body> 12</html>
insert でなく replace ですが、変数でファイル名を指定出来ています。
<div th:insert="../static/html/${htmlFilename}::body"> ではうまく行きませんでした。
というのが具体的どういう結果だったのか(何かエラーなのか空白なのかそれとも何か…)分かりませんが、少なくとも動的には出来るようですので、パスも変数に含ませてみるとか、パスを html に書かないといけないならパラメータ全体をリテラル置換で編集してみるとかで何とかなりそうな気がします。
投稿2024/03/02 08:35
編集2024/03/03 07:40総合スコア13318
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。