Back-end/Springboot
스프링 Formatter 사용
개발자 키우기
2023. 7. 12. 18:24
스프링이 제공하는 어노테이션 기반 포맷터를 사용하면 원하는 객체의 필드에 원하는 포맷형식을 지정할 수 있다
코드는 아래와 같다
TypeConverterController
@Controller
public class TypeConverterController {
@GetMapping("/item")
public String addItemForm(Model model){
Item item = new Item("Clean Code", 30000, LocalDateTime.now());
model.addAttribute("item", item);
return "addItemForm";
}
@PostMapping("/item")
public String addItem(@ModelAttribute Item item){
return "item";
}
@Data
@AllArgsConstructor
static class Item {
private String name;
@NumberFormat(pattern = "###,###")
private Integer price;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createDateTime;
}
}
public class TypeConverterController {
@GetMapping("/item")
public String addItemForm(Model model){
Item item = new Item("Clean Code", 30000, LocalDateTime.now());
model.addAttribute("item", item);
return "addItemForm";
}
@PostMapping("/item")
public String addItem(@ModelAttribute Item item){
return "item";
}
@Data
@AllArgsConstructor
static class Item {
private String name;
@NumberFormat(pattern = "###,###")
private Integer price;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createDateTime;
}
}
addItemForm.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:object="${item}" th:method="post">
name <input type="text" th:field="*{name}"><br/>
price <input type="text" th:field="*{price}"><br/>
createDateTime <input type="text" th:field="*{createDateTime}"><br/>
<input type="submit"/>
</form>
</body>
</html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:object="${item}" th:method="post">
name <input type="text" th:field="*{name}"><br/>
price <input type="text" th:field="*{price}"><br/>
createDateTime <input type="text" th:field="*{createDateTime}"><br/>
<input type="submit"/>
</form>
</body>
</html>
item.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li>${{item.name}}: <span th:text="${{item.name}}" ></span></li>
<li>${{item.price}}: <span th:text="${{item.price}}" ></span></li>
<li>${{item.createDateTime}}: <span th:text="${{item.createDateTime}}" ></span></li>
</ul>
</body>
</html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li>${{item.name}}: <span th:text="${{item.name}}" ></span></li>
<li>${{item.price}}: <span th:text="${{item.price}}" ></span></li>
<li>${{item.createDateTime}}: <span th:text="${{item.createDateTime}}" ></span></li>
</ul>
</body>
</html>
결과
</div th:text="${#temporals.format(item.createdatetime, 'yyyy-mm-dd hh:mm:ss')}">
</div th:text="${#dates.format(item.createdatetime, 'yyyy-mm-dd hh:mm:ss')}">
위에 방식처럼 타임리프에서도 날짜형식 변경이나 포멧등을 지원해 주지만 수많은 페이지에서 해당 포맷이 필요하다면
스프링이 제공하는 포멧을 사용하는 것이 효율 적일 것이다.
또한 Item 객체는 입출력을 담당하면서 포멧 형식을 적용하고
ItemAdd 객체와 ItemUpdate 객체를 따로 만들어 생성과 수정을 담당하면서 검증을 수행하도록 지정하면 좋다
참고로 JSON은 메시지 컨버터가 적용되지 않기 때문에 따로 설정을 해야 함