
<스프링 MVC 백엔드 웹개발 핵심 개발 기술> 에서 배운 것들
@RequiredArgsConstructor
final이 붙은 멤버변수만 사용해서 생성자를 자동으로 만들어준다. public BasicItemController(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
@Autowired로 의존관계를 주입해준다. 따라서 final 키워드를 빼면 안된다!@PostConstruct
타임리프 사용 선언
<html xmlns:th="http://www.thymeleaf.org">
속성 변경 - th:href
th:href="@{/css/bootstrap.min.css}"
href="value1을 th:href="value2"의 값으로 변경한다.th:xxx값으로 변경한다. 만약 값이 없다면 새로 생성한다.href속성이 사용되고, 뷰 템플릿을 거치면 th:href의 값이 href로 대체되면서 동적으로 변경할 수 있다.th:xxx로 변경할 수 있다.타임리프 핵심
th:xxx가 붙은 부분은 서버사이드에서 렌더링되고, 기존 것을 대체한다. th:xxx이 없으면 HTML의 xxx속성이 그대로 사용된다.URL 링크 표현식 - @{...}
th:href="@{/css/bootstrap.min.css}"
@{...}타임리프는 URL 링크를 사용하는 경우, @{...}를 사용한다. 속성 변경 - th:onclick
onclick="location.href='addForm.html'"th:onclick="|location.href='@{/basic/items/add}'|"리터럴 대체 문법 - |...|
|...| 이렇게 사용한다
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"><span th:text="|Welcome to our application, ${user.name}!|">location.href='/basic/items/add' 이렇게 만드려면th:onclick="'location.href=' + '\'' + @{/basic/items/add} + '\''"th:onclick="|location.href='@{/basic/items/add}'|"반복 출력 - th:each
<tr th:each="item : ${items}">th:each를 사용한다. 이렇게 하면 모델에 포함된 items컬렉션 데이터가 item변수에 하나씩 포함되고, 반복문 안에서 item변수를 사용할 수 있다.<tr>..</tr>이 하위 태그를 포함해서 생성된다.변수 표현식 - ${...}
<td th:text="${item.price}">10000</td>item.getPrice())내용 변경 - th:text
-<td th:text="${item.price}">10000</td>
th:text의 값으로 변경한다.${item.price}의 값으로 변경한다.URL 링크 표현식 2 @{...}
th:href="@{/basic/items/{itemId}(itemId=${item.id})}"{itemId}) 뿐만 아니라 쿼리 파라미터도 생성한다.th:href="@{/basic/items/{itemId}(itemId=${item.id}, query='test')}"http://localhost:8080/basic/items/1?query=test간단한 URL 링크
th:href="@{|/basic/items/${item.id}|}"