resourceHandler는 요청을 default servlet에게 위임하고 default servlet이 resource를 제공한다.
Spring MVC에서 필요한 Bean을 등록하기 위해 사용한다.
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/")
.setCachePeriod(60)
.resourceChain(true)
.addResolver(new EncodedResourceResolver());
}
setCachePeriod()
캐싱 설정
resourceChain(true)
resourceResolver에 요청에 따른 자원 매핑 설정(기본 resolver는 pathResolver지만 chain을 사용하면 우선 순위 떨어짐)
.resourceChain(true).addResolver(new EncodedResourceResolver())
@GetMapping("/customers")
public String findCustomers(Model model) {
var allCustomers = customerService.getAllCustomers();
model.addAttribute("serverTime", LocalDateTime.now());
model.addAttribute("customers", allCustomers);
return "views/customers";
}
<p th:text="'The time on the server is' + ${serverTime}">----</p>
<img th:src="@{/resources/bg.png}" class="img-fluid">
<tr th:each="customer: ${customers}" th:object="${customer}" >
<td th:text="${customer.customerId}"></td>
<td th:text="*{name}"></td>
<td th:text="*{email}"></td>
<td th:text="*{createdAt}"></td>
<td th:text="*{lastLoginAt}"></td>
</tr>