뷰 템플릿에 적용하기
@Controller
public class ConverterController {
@GetMapping("/converter-view")
public String converterView(Model model) {
model.addAttribute("number", 10000);
model.addAttribute("ipPort", new IpPort("127.0.0.1", 8080));
return "converter-view";
}
}<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li>${number}:<span th:text="${number}"></span></li>
<li>${{number}}:<span th:text="${{number}}"></span></li>
<li>${ipPort}:<span th:text="${ipPort}"></span></li>
<li>${{ipPort}}:<span th:text="${{ipPort}}"></span></li>
</ul>
</body>
</html>${number}: 10000
${{number}}: 10000
${ipPort}: hello.typeconverter.type.IpPort@59cb0946
${{ipPort}}: 127.0.0.1:8080
뷰 템플릿에도 {{number}} 형태로 데이터를 문자로 컨버팅 해 출력할 수 있다.
Form에 적용하기
Form에도 적용 가능하다.
converterForm()
th:field가 자동으로 컨버전 서비스를 적용해
${{ipPort}}를 해준 것처럼 IpPort에서 String 타입이 되었다.
converterEdit()
@ModelAttribute가 내부적으로 컨버전 서비스를 사용해 String에서 IpPort로 변환한다.
Last updated
Was this helpful?