Spring-Thymelafプリファレンス


簡単な説明


It's way of configuration of spring as use thymeleaf.

開発環境(IDE、言語、フレームワーク等)

  • IDE : intiliJ
  • language : JAVA11, HTML5
  • framework : Spring 5.2.3.RELEASE
  • template engine : thymeleaf 3.0.11.RELEASE
  • プロジェクト構造

    │  base-set.iml
    │  pom.xml
    │  
    └─src
        ├─main
        │  ├─java
        │  │  └─com
        │  │      └─base
        │  │          └─example
        │  │                  HelloController.java
        │  │                  
        │  ├─resources
        │  │      logback.xml
        │  │      
        │  └─web
        │      ├─resources
        │      │  ├─css
        │      │  └─js
        │      └─WEB-INF
        │          │  applicationContext.xml
        │          │  dispatcher-servlet.xml
        │          │  web.xml
        │          │  
        │          └─views
        │                  hello.html
        │                  
        └─test
            └─java

    敵意

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.base.example</groupId>
        <artifactId>base-set</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <jdk.version>11</jdk.version><!--기본설정-->
            <spring.version>5.2.3.RELEASE</spring.version><!--기본설정-->
            <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version><!--기본설정-->
            <jcloverslf4j.version>1.7.31</jcloverslf4j.version><!--기본설정-->
            <logback.version>1.2.3</logback.version><!--기본설정-->
        </properties>
    
        <dependencies>
            <!--기본설정-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!--기본설정-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <!--기본설정-->
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf-spring5</artifactId>
                <version>${thymeleaf.version}</version>
            </dependency>
    
            <!--기본설정-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>RELEASE</version>
                <scope>compile</scope>
            </dependency>
    
            <!--기본설정-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${jcloverslf4j.version}</version>
            </dependency>
    
            <!--기본설정-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <!--디펜던시 기본설정-->
    
    
        </dependencies>
    
    
        <build>
            <plugins>
                <!--플러그인 기본설정 시작-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                    </configuration>
                </plugin>
                <!--플러그인 기본설정 끝-->
            </plugins>
        </build>
    </project>

    web.xml設定

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
             http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <!--기본설정 시작-->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--dispatcher 에 들어갈 지역변수-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!--기본설정 종료-->
    
    
    </web-app>

    アプリケーションContext設定

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--기본설정 시작-->
        <import resource="dispatcher-servlet.xml"/>
    
        <context:component-scan base-package="com.base.*"/>
        <!--기본설정 끝-->
    
    
    </beans>

    dispatcher-servlet設定

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--spring - thymeleaf 기본설정 시작 -->
        <mvc:annotation-driven/>
    
        <mvc:resources mapping="/resources/**" location="/resources/"/>
    
        <bean id="templateResolver"
              class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".html" />
            <!--뷰리졸버에서 utf-8로 반환할것을 설정-->
            <property name="characterEncoding" value="utf-8" />
            <property name="templateMode" value="HTML5" />
        </bean>
    
        <!--바로위의 id="templateResolver" 설정을 가져와서 타임리프에 적용-->
        <bean id="templateEngine"
              class="org.thymeleaf.spring5.SpringTemplateEngine">
            <property name="templateResolver" ref="templateResolver" />
        </bean>
    
        <!--templateEngine bean을 매개로 타임리프 엔진에 templateResolver 의 설정을 전달-->
        <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="templateEngine" ref="templateEngine" />
            <!--뷰 리졸버에서 온 자료를 utf-8 형식으로 출력하게 설정-->
            <property name="characterEncoding" value="utf-8"/>
        </bean>
        <!--spring - thymeleaf 기본설정 끝-->
    
    
    </beans>

    logback設定

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <!--기본설정-->
        <!-- 콘솔로 로그를 남김 -->
        <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <charset>UTF-8</charset>
                <!-- 로그 메시지 패턴 -->
                <Pattern>
                    %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
                </Pattern>
            </encoder>
        </appender>
    
        <!--기본설정-->
        <!-- 파일로 로그를 남김 -->
        <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>D:\basic-setting-projects\spring-thymleaf\base-set\logexample2.log</file>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>
                    %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
                </Pattern>
            </encoder>
    
            <!--기본설정-->
            <!-- 로그를 남기는 파일의 용량이 50KB가 넘으면 이를 압축 파일로 만들고 새로 로그 파일로 만들라는 정책 -->
            <triggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>10KB</MaxFileSize>
            </triggeringPolicy>
    
            <!--기본설정-->
            <!-- 파일을 덮어쓰는 정책 -->
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <FileNamePattern>DD:\basic-setting-projects\spring-thymleaf\base-set\logexample2.%i.log.zip</FileNamePattern>
                <!--
                    MinIndex가 1이고, MaxIndex가 10이므로, 위의 파일 이름 패턴에 따라 아래의 로그 파일이 생길 것이다.
                    logexample2.1.log.zip   logexample2.2.log.zip  .... logexample2.10.log.zip
                    이 상태에서 또 10KB가 넘으면 logexample2.1.log.zip이 된다.
                -->
                <MinIndex>1</MinIndex>
                <MaxIndex>10</MaxIndex>
            </rollingPolicy>
        </appender>
    
        <!--기본설정-->
        <!--
                 com.base.example 아래 패키지 로그들만  consoleAppender, fileAppender 방법으로 로그를 남긴다.
                 물론 <appender-ref ref="consoleAppender" />를 추가하여 콘솔로도 로그를 남길 수 있다.
        -->
        <logger name="com.base.example"  level="info" additivity="false">
            <appender-ref ref="consoleAppender" />
        </logger>
    
        <!--기본설정-->
        <!-- root는 글로벌 로거를 의미하며, 위의 logger에 해당하지 않으면 root 로거가 실행된다. -->
        <root level="warn">
            <appender-ref ref="consoleAppender" />
        </root>
    
    
    </configuration>

    コントローラの設定

    package com.base.example;
    
    import org.apache.commons.logging.Log;//기본임포트
    import org.apache.commons.logging.LogFactory;//기본임포트
    import org.springframework.stereotype.Controller;//기본임포트
    import org.springframework.ui.Model;//기본임포트
    import org.springframework.web.bind.annotation.GetMapping;//기본임포트
    import org.springframework.web.bind.annotation.RequestParam;//기본임포트
    
    @Controller
    public class HelloController {
        private static final Log LOG = LogFactory.getLog(HelloController.class );
    
        @GetMapping({"/"})
        public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
            model.addAttribute("name", name);
            LOG.info("hello 가동");
            return "hello";
        }
        /*여기까지 기본설정*/
    }

    サービス設定(Optional)


    -None-

    DAO設定(オプション)


    -None-

    DTO設定(オプション)


    -None-

    MybatisとJPA設定(Optinal)


    -None-

    Javascript設定


    -None-

    Html設定

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="ko">
    <head>
        <meta charset="utf-8"/>
        <title th:text="'Hello ' + ${name} + '!'"></title>
        <link th:href="@{/resources/css/main.css}" rel="stylesheet"/>
    </head>
    <body>
    <h2 class="hello-title" th:text="'Hello ' + ${name} + '!'"></h2>
    <h3>쿨럭</h3>
    <script th:src="@{/resources/js/main.js}"></script>
    </body>
    </html>

    システムの環境設定



    結果


    エクスプローラ

    コンソール