[Thymeleaf] Controller 반환 타입(String타입 - templates밑의 html파일 명과 매핑되는 이유)

Ogu·2022년 12월 27일
0

SpringBoot

목록 보기
5/17
post-thumbnail
post-custom-banner

스프링 Controller에서 RequestMapping 을 하는 것에 있어서 어떻게 String return 값이 파일명으로 자동 매핑이 되는지, 왜 templates 폴더 밑에 작성하는지(default)값이 궁금했다.

Controller의 리턴 타입

  • String 리턴 타입
    Spring + View template을 사용할 때 흔히 사용하는 타입이다.
  • void 리턴 타입
    해당 URL 정보를 그대로 jsp 파일 이름으로 사용한다.

String 리턴 타입 예시

아래 코드를 보면 return되는 문자열 은 index 로 이는 resources 폴더 아래 만들어둔 templates > index.html 파일로 연결된다.

@Controller
public class MainPage {
    @RequestMapping("/")
    public String index() {
        return "index";
    }  
    

왜 templates폴더 밑의 html파일과 Mapping되는 거지?

이러한 설정은 YAML 파일에서 개인이 따로 설정이 가능하다.
이때 thymeleaf 자동 완성을 살펴보면 default로 설정된 값들이 보인다.

ThymeleafProperties 클래스를 직접 들어가서 살펴보자.

Thymeleaf Properties

  • DEFAULT_PREFIX가 "classpath:/templates/" 로 설정되어있고,
  • DEFAULT_SUFFIX가 ".html" 로 설정되어있는 것을 볼 수 있다.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.boot.autoconfigure.thymeleaf;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType;
import org.springframework.util.unit.DataSize;

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
    private boolean cache;
    private Integer templateResolverOrder;
    private String[] viewNames;
    private String[] excludedViewNames;
    private boolean enableSpringElCompiler;
    private boolean renderHiddenMarkersBeforeCheckboxes;
    private boolean enabled;
    private final ThymeleafProperties.Servlet servlet;
    private final ThymeleafProperties.Reactive reactive;

    public ThymeleafProperties() {
        this.encoding = DEFAULT_ENCODING;
        this.cache = true;
        this.renderHiddenMarkersBeforeCheckboxes = false;
        this.enabled = true;
        this.servlet = new ThymeleafProperties.Servlet();
        this.reactive = new ThymeleafProperties.Reactive();
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isCheckTemplate() {
        return this.checkTemplate;
    }

    public void setCheckTemplate(boolean checkTemplate) {
        this.checkTemplate = checkTemplate;
    }

    public boolean isCheckTemplateLocation() {
        return this.checkTemplateLocation;
    }

    public void setCheckTemplateLocation(boolean checkTemplateLocation) {
        this.checkTemplateLocation = checkTemplateLocation;
    }

    public String getPrefix() {
        return this.prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return this.suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getMode() {
        return this.mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public Charset getEncoding() {
        return this.encoding;
    }

    public void setEncoding(Charset encoding) {
        this.encoding = encoding;
    }

    public boolean isCache() {
        return this.cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public Integer getTemplateResolverOrder() {
        return this.templateResolverOrder;
    }

    public void setTemplateResolverOrder(Integer templateResolverOrder) {
        this.templateResolverOrder = templateResolverOrder;
    }

    public String[] getExcludedViewNames() {
        return this.excludedViewNames;
    }

    public void setExcludedViewNames(String[] excludedViewNames) {
        this.excludedViewNames = excludedViewNames;
    }

    public String[] getViewNames() {
        return this.viewNames;
    }

    public void setViewNames(String[] viewNames) {
        this.viewNames = viewNames;
    }

    public boolean isEnableSpringElCompiler() {
        return this.enableSpringElCompiler;
    }

    public void setEnableSpringElCompiler(boolean enableSpringElCompiler) {
        this.enableSpringElCompiler = enableSpringElCompiler;
    }

    public boolean isRenderHiddenMarkersBeforeCheckboxes() {
        return this.renderHiddenMarkersBeforeCheckboxes;
    }

    public void setRenderHiddenMarkersBeforeCheckboxes(boolean renderHiddenMarkersBeforeCheckboxes) {
        this.renderHiddenMarkersBeforeCheckboxes = renderHiddenMarkersBeforeCheckboxes;
    }

    public ThymeleafProperties.Reactive getReactive() {
        return this.reactive;
    }

    public ThymeleafProperties.Servlet getServlet() {
        return this.servlet;
    }

    static {
        DEFAULT_ENCODING = StandardCharsets.UTF_8;
    }

    public static class Reactive {
        private DataSize maxChunkSize = DataSize.ofBytes(0L);
        private List<MediaType> mediaTypes;
        private String[] fullModeViewNames;
        private String[] chunkedModeViewNames;

        public Reactive() {
        }

        public List<MediaType> getMediaTypes() {
            return this.mediaTypes;
        }

        public void setMediaTypes(List<MediaType> mediaTypes) {
            this.mediaTypes = mediaTypes;
        }

        public DataSize getMaxChunkSize() {
            return this.maxChunkSize;
        }

        public void setMaxChunkSize(DataSize maxChunkSize) {
            this.maxChunkSize = maxChunkSize;
        }

        public String[] getFullModeViewNames() {
            return this.fullModeViewNames;
        }

        public void setFullModeViewNames(String[] fullModeViewNames) {
            this.fullModeViewNames = fullModeViewNames;
        }

        public String[] getChunkedModeViewNames() {
            return this.chunkedModeViewNames;
        }

        public void setChunkedModeViewNames(String[] chunkedModeViewNames) {
            this.chunkedModeViewNames = chunkedModeViewNames;
        }
    }

    public static class Servlet {
        private MimeType contentType = MimeType.valueOf("text/html");
        private boolean producePartialOutputWhileProcessing = true;

        public Servlet() {
        }

        public MimeType getContentType() {
            return this.contentType;
        }

        public void setContentType(MimeType contentType) {
            this.contentType = contentType;
        }

        public boolean isProducePartialOutputWhileProcessing() {
            return this.producePartialOutputWhileProcessing;
        }

        public void setProducePartialOutputWhileProcessing(boolean producePartialOutputWhileProcessing) {
            this.producePartialOutputWhileProcessing = producePartialOutputWhileProcessing;
        }
    }
}

참고 URL

profile
Hello! I am Ogu, a developer who loves learning and sharing! 🐤🐤 <br> こんにちは!学ぶことと共有することが好きな開発者のOguです!🐤
post-custom-banner

0개의 댓글