부트 스트랩에서 위와 같은 요소를 복붙 하려고 한다.
소스 코드를 보면
<div id="carouselExampleCaptions" class="carousel slide" data-bs-ride="false">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="1" aria-label="Slide 2"></button>
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="2" aria-label="Slide 3"></button>
</div>
<div class="carousel-inner">
<!-- 반복코드 -->
<div class="carousel-item active">
<img src="..." class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>First slide label</h5>
<p>Some representative placeholder content for the first slide.</p>
</div>
</div>
<!-- 반복코드 끝 -->
<div class="carousel-item">
<img src="..." class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Second slide label</h5>
<p>Some representative placeholder content for the second slide.</p>
</div>
</div>
<div class="carousel-item">
<img src="..." class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Third slide label</h5>
<p>Some representative placeholder content for the third slide.</p>
</div>
</div>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
위의 반복 코드 부분을 thymeleaf의 반복문으로 생성하려고 했다.
안의 이미지 속성들을 각 요소의 값으로 채우면서 말이다.
그런데 문제가 있다.
보면 첫번째 요소만 class 속성에 active 속성이 추가되어있다.
반복중에 몇번째 요소에만 추가적인 속성을 적용할 수 있는가?
아래의 코드를 이용하면 된다. 아래의 코드는 나의 코드의 일부분이다. inner부분만 가져왔다
<div class="carousel-inner">
<!-- repeat -->
<th:block th:each="file:${boardDto.fileList}">
<div class="carousel-item" th:classappend="${boardDto.fileList[0].idx}==${file.idx} ? 'active' : ''">
<img th:src="|/board/getImage?img_idx=${file.idx}|" class="d-block w-100" alt="Responsive image">
<div class="carousel-caption d-none d-md-block">
<h5 th:text="${file.originalFileName}"></h5>
<p th:text="|${file.originalFileName} (${file.fileSize} kb) by ${file.creatorId}|"></p>
</div>
</div>
</th:block>
</div>
이 코드를 보면 thymeleaf의 꽤 많은 문법을 공부할 수 있다.
th:block
<th:block th:each="file:${boardDto.fileList}">
each문과 비슷하게 반복을 해준다. 다른 점은 block은 계층이 다른 요소들이 뒤섞여있는 곳에서 쓰기 좋다.
th:classappend
<div class="carousel-item" th:classappend="${boardDto.fileList[0].idx}==${file.idx} ? 'active' : ''">
th:attr와 비슷하다. 하지만 class에 값을 추가시킬때는 이 문법을 써야한다
"${boardDto.fileList[0].idx}==${file.idx} ? 'active' : ' ' "
<div class="carousel-item" th:classappend="${boardDto.fileList[0].idx}==${file.idx} ? 'active' : ''">
이 부분은 꽤나 핵심적인 문법이 많이 들어가 있다.
일단 삼항 연산자는 "조건식 ? '참의 값' : '거짓의 값'" 이렇게 나타낸다.
그리고 그 안에 비교 연산자 == 이 있다. 자바와 똑같이 삼항연산자는 안의 값들이 모두 계산된 뒤에 연산이 시작된다.
그리고 그 안에 boardDto.fileList라는 List<>에서 직접 인덱싱을 해줄 수 있다.
- Object
- data.field : data의 field 프로퍼티 접근 ( data.getField())
- data['field'] : 위와 같다.
- data.getField() : data의 getField 메서드를 바로 호출 가능
- List
- list[0].field : List의 첫번째 요소에서 field 프로퍼티를 가져올 수 있다.
- list[0]['field'] : 위와 동일
- list[0].getField() : 메서드 직접 호출도 가능
- list.get(0).xxx : List의 get메서드를 사용하여 데이터를 찾아서 프로퍼티 접근도 가능하다. - Map
- map['key'].field : Map에서 key를 찾아 field프로퍼티에 접근한다. ( map.get('key').field와 동일)
- map['key']['field'] : 위와 동일
- map['key'].getField() : 메서드 호출 가능
이를 참고하자.
그리고 그 인덱싱 된 것을 바로 .idx로 안의 필드를 가져올 수 있다.
block 안에서 each를 돌린 file이라는 변수를 ${file.idx}로 가져와서 비교 할 수 있다.
실행결과
<div class="carousel-inner">
<!-- repeat -->
<div class="carousel-item active">
<img src="/board/getImage?img_idx=2" class="d-block w-100" alt="Responsive image">
<div class="carousel-caption d-none d-md-block">
<h5>IMG_2035.JPG</h5>
<p>IMG_2035.JPG (2503 kb) by test</p>
</div>
</div>
<div class="carousel-item">
<img src="/board/getImage?img_idx=3" class="d-block w-100" alt="Responsive image">
<div class="carousel-caption d-none d-md-block">
<h5>IMG_2035 2.JPG</h5>
<p>IMG_2035 2.JPG (2503 kb) by test</p>
</div>
</div>
<div class="carousel-item">
<img src="/board/getImage?img_idx=4" class="d-block w-100" alt="Responsive image">
<div class="carousel-caption d-none d-md-block">
<h5>IMG_2035 3.JPG</h5>
<p>IMG_2035 3.JPG (2503 kb) by test</p>
</div>
</div>
</div>
매우 잘 실행되고 첫번째 요소에만 active가 추가로 붙었다.
'SpringBoot > [SpringBoot]' 카테고리의 다른 글
[SpringBoot] ModelAndView.addObject 의 기본 이름 부여 (0) | 2022.11.29 |
---|---|
[SpringBoot] 파일 다운로드의 두 가지 방법 (0) | 2022.11.24 |
[SpringBoot] 웹에 이미지를 표시하는 두가지 방법 (0) | 2022.11.17 |
[SpringBoot] 로컬 파일 저장과 DB를 같이 트랜잭션 시키기 (0) | 2022.11.16 |
[SpringBoot] @Transactional 의도적 에러 발생시키기 (0) | 2022.11.16 |