Dev/Spring

Thymeleaf에서 스프링의 각종 환경변수,파라미터 등 읽어오는 법.

마이스토리 2020. 7. 13. 16:20

참고 : https://java.ihoney.pe.kr/472

참고 : https://eblo.tistory.com/56

 

# application.properties 값 조회

@ 뒤에 빈(Bean) 이름을 사용하면 그 빈에 접근할 수 있다.

<span th:text="${@environment.getProperty('app.title')}"></span>

프로파일 환경에 따라 표시를 하려면

<div th : if = $ { @environment .acceptsProfiles ( 'production' )}>
    This is the production profile
</ div>
또는
<div th : if = "$ {# arrays.contains (@ environment.getActiveProfiles () 'production')} " >
     This is the production profile
</ div>

# 시스템 환경변수 조회

${@systemProperties['property.key']}

# 메세지 조회

<span th:text="#{msg.example.title}"></span>

# 세션정보 조회

<span th:text="${session['userId']}"></span> 

<span th:text="${session.userId}"></span> 

# 파라미터 조회

<span th:text="${param.authType}"></span> 

<span th:text="${#httpServletRequest.getParameter('authType')}"></span> 

# PathVariable 조회

<span th:text="__${userId}__"></span>

* Spring 컨트롤러에 Request Mapping에 선언되어 있고 @PathVariable이 있어야만 정보를 가져올 수 있다.