본문 바로가기
개발지식

Thymeleaf - 서버에서 전달된 값 select box에 선택하기

by devLog by Ronnie's 2021. 9. 28.

들어가며


앞단 작업을 하다가 컨트롤러에서 넘겨준 값을 select 박스에 반영을 했어야 했는데 좀 헤매다가 방법을 찾아서 남겨둔다.

 

해결방법


먼저 컨트롤러에서 값을 모델에  담아 전달한다.

input 박스 같은 경우는 간단히 th:value를 이용하면 값이 들어가는 것을 확인할 수 있다.

 <input type="text" name="email" th:value="${contact.email}" id="email">

 

 

 

 

하지만 select box 같은 경우는 th:value만 한다고 값이 바뀌지 않는다. 

selected를 추가를 해줘야하는데 방법은 다음과 같다.

 

<select name="roomCnt" id="roomCnt" th:value="${contact.roomCnt}">
	<option th:selected="${contact.roomCnt}=='1'">1개</option>
	<option th:selected="${contact.roomCnt}=='2'">2개</option>
    <option th:selected="${contact.roomCnt}=='3'">3개</option>
</select>

 

 

 

 

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

댓글