• Controller
@GetMapping("/m7")
	public String m7(Model model) {
		int num1= 1234567;
		double num2 = 12345.6789;
		Calendar now = Calendar.getInstance();
		model.addAttribute("num1",num1);
		model.addAttribute("num2",num2);
		model.addAttribute("now",now);
		return "m7";
}

@GetMapping("/m8")
	public String m8(Model model) {
		int seq = 10;
		String mode = "add";
		model.addAttribute("seq",seq);
		model.addAttribute("mode",mode);
		return "m8";
}
	
@GetMapping("/m9")
public String m9(Model model) {
    int num1 = 10;
    int num2 = 5;
    String mode = "add";
    model.addAttribute("num1", num1);
    model.addAttribute("num2", num2);
    model.addAttribute("mode", mode);
    return "m9";
}
  • 숫자 형식 변환하기 m7.html
    • #numbers.formatDecimal(표시 값, 최소 정수 자릿수, 최소 소수 자릿수)
    • 추가로 가능한 것
      • formatCurrency, formatPercent 이런것도 가능하다. 
      • ${#numbers.arrayFormatDecimal(numArray,3,2)}
      • ${#numbers.listFormatDecimal(numList,3,2)}
      • ${#numbers.setFormatDecimal(numSet,3,2)}
<h2>숫자</h2>
<div th:text="${num1}"></div>
<div th:text="${#numbers.formatInteger(1, 3, 'COMMA')}"></div>    
<!-- 남은 자리 0으로 채운다. 최대 3자리 --> 
<div th:text="${#numbers.formatCurrency(1)}"></div>
<div th:text="${#numbers.formatPercent(num1, 5, 1)}"></div>
<div th:text="${#numbers.formatInteger(num1, 3, 'COMMA')}"></div>
<!-- 두번쨰 인자는 표현자리수다. -->
<div th:text="${num2}"></div>
<div th:text="${#numbers.formatDecimal(num2, 3, 'COMMA', 1, 'POINT')}"></div>
<!-- 3자리씩콤마로 짜르고, 1자리 소수부분 나타낸다. -->

 

결과

  • 날짜관련
    • #dates.메서드로 날짜 객체를 받아서 사용한다.
<h2>날짜</h2>
<div th:text="${now}"></div> 
<div th:text="${#dates.year(now)}"></div>
<div th:text="${#dates.month(now)}"></div>
<div th:text="${#dates.monthName(now)}"></div>
<div th:text="${#dates.monthNameShort(now)}"></div>
<div th:text="${#dates.day(now)}"></div>
<div th:text="${#dates.hour(now)}"></div>
<div th:text="${#dates.minute(now)}"></div>
<div th:text="${#dates.second(now)}"></div>
<div th:text="${#dates.millisecond(now)}"></div>
<div th:text="${#dates.dayOfWeek(now)}"></div>
<div th:text="${#dates.dayOfWeekName(now)}"></div>
<div th:text="${#dates.dayOfWeekNameShort(now)}"></div>
<div th:text="${#dates.format(now)}"></div>
<div th:text="${#dates.format(now, 'yyyy-MM-dd HH:mm:ss')}"></div>
<div th:text="${#dates.format(now, 'yyyy-MM-dd aa hh:mm:ss')}"></div>

결과

  • Link URL Expression
    • @{   }  
    • a태그에서 URL 표현
      • 매개변수 처리가 쉽고 Context Root Path가 자동으로 삽입된다. 
      • 쿼리 스트링을 붙이는 표현 가능
      • ★★★ th:href와 같이 쓴다. 
      • @{url(key1=value1, key2=value2)}
  • m8.html
 <div><a href="/m7">이전 페이지</a></div>
 <div><a href="/spring/m7">이전 페이지</a></div>

 <!-- root context를 붙여준다. -->
 <div><a th:href="@{/m7}">이전 페이지</a></div>
 <hr>
 <h3>QueryString, 매개변수</h3>
 <div>
    <a href="/m7?seq=100">이전 페이지</a>
    <a href="/m7?seq=${seq}">이전 페이지</a> <!-- 잘못된 표현 -->
    <a th:href="@{/m7(seq=${seq})}">이전 페이지</a>

    <a href="/m7?seq=100&mode=add">이전 페이지</a>
    <a th:href="@{/m7(seq=${seq}, mode=${mode})}">이전 페이지</a>
 </div>
 <!-- 
    기본 앱(QueryString)
    - /m7?seq=10

    REST(Path Variable)
    - /m7/10
  -->
 <div>
    <!-- 바인딩이 되버린다. -->
    <a th:href="@{/m7/{seq}(seq=${seq})}">이전 페이지</a>
    <a th:href="@{/m7/{mode}/{seq}(seq=${seq},mode=${mode})}">이전 페이지</a>
 </div>

소스검사

  • th:if, th:unless
    • 조건을 만족하면 해당 태그가 만들어지고 만족하지 못하면 해당 태그가 사라진다. 
    • if와 else 연달아서는 아래처럼 쓴다.
      • <div th:if="${num1 > 0}">양수</div>
      •  <div th:unless="${num1 > 0}">음수</div>
  • th:switch
    • th:switch로 받아서 th:case로 매칭
  • m9.html
 <h2>if</h2>
 <div th:if="${num1 > 0}">num1 : 양수</div>
 <div th:if="${num2 < 0}">num2 : 양수</div>
 <div th:if="${num1 > 0}">
    <span th:text="'양수' + ${num1} + '입니다.'"></span>
 </div>
 <!-- 조건을 만족못하면 테그 자체가 사라진다. 조건을 만족하면 태그가 만들어진다. -->
 <div th:if="${num1 > 0}" th:text="'양수' + ${num1} + '입니다.'"></div>
 <div th:if="${num1 > 0}" th:text="|양수 + ${num1} + 입니다.|"></div>
 <div th:if="${num1 > 0}">양수 [[${num1}]]입니다.</div>
 <hr>	 
 <div th:if="${num1 > 0}">양수</div>
 <div th:unless="${num1 > 0}">음수</div>
 <!-- 이걸 만족하지 못했을 때 실행된다. -->
 <hr>
 <h2>switch</h2>
 <div th:switch="${mode}">
    <div th:case="add">추가하기</div>
    <div th:case="remove">삭제하기</div>
    <div th:case="*">기타</div>
 </div>

소스검사

+ Recent posts