0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Thymeleafの設定(spring bootなし)

Last updated at Posted at 2024-07-19

はじめに

spring bootに頼らないspring MVCプロジェクトのThymeleafの設定方法の備忘録

プロジェクト自体の作成方法は下記を参考
MavenによるSpringプロジェクトの作成 #Java - Qiita

設定方法

pom.xml

pom.xmlには下記を追加してあげればよい

    ・・・
    <!-- Thymeleaf -->
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring6</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>
    ・・・

Java Config設定

Thymeleaf用のJava Configの設定です

下記3つをBeanに登録します

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringResourceTemplateResolver templateResolver(){
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        // テンプレートファイルがどのディレクトリの配下にあるかを指定。ここでは、/WEB-INF/views/配下
        templateResolver.setPrefix("/WEB-INF/views/");
        // 接尾字が何かを指定。ここでは.html
        templateResolver.setSuffix(".html");
        // テンプレートの種類は何かを指定。ここでhtml
        templateResolver.setTemplateMode(TemplateMode.HTML);
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine(){
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver viewResolver(){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        // ViewResolverを使う順序を指定。JSPより先にする。
        viewResolver.setOrder(1);
        return viewResolver;
    }

}

今回作成したプロジェクトでは初めにJSPを読み込ませているためviewResolver.setOrder(1)を設定することでJSPより優先させています。JSPとの共存も可能なようです。

@Importを利用してDispatcherServletクラスにThymeleafConfig.classをインポートします

package com.example.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan("com.example.demo")
@Import(ThymeleafConfig.class)
public class DispatcherServlet implements WebMvcConfigurer {
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp();
    }
}

あとは通常通り/WEB-INF/views/配下にhtmlファイルを作成してThymeleafが利用できます。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>テスト</title>
</head>
<body>
    <div th:text="${title}">テキスト</div>
</body>
</html>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?