스프링/타임리프

타임리프 속성값

수연정 2024. 2. 6. 10:53

속성설정

<input type="text" name="mock" th:name="userA" />

th:* 속성을 지정하면 타임리프는 기존 속성을 th:* 로 지정한 속성으로 대체한다. 기존 속성이 없다면 새로 만든다.
<input type="text" name="mock" th:name="userA" />
타임리프 렌더링 후 <input type="text" name="userA" />

속성추가

- th:attrappend = <input type="text" class="text" th:attrappend="class=' large'" /><br/>
- th:attrprepend = <input type="text" class="text" th:attrprepend="class='large '" /><br/>
- th:classappend = <input type="text" class="text" th:classappend="large" /><br/>

th:attrappend : 속성 값의 뒤에 값을 추가한다.
th:attrprepend : 속성 값의 앞에 값을 추가한다.
th:classappend : class 속성에 자연스럽게 추가한다.

CHECKED 처리

- checked o <input type="checkbox" name="active" th:checked="true" /><br/>
- checked x <input type="checkbox" name="active" th:checked="false" /><br/>
- checked=false <input type="checkbox" name="active" checked="false" /><br/>

HTML에서는 <input type="checkbox" name="active" checked="false" /> 이 경우에도

checked 속성이 있기 때문에 checked 처리가 되어버린다.


HTML에서 checked 속성은 checked 속성의 값과 상관없이 checked 라는 속성만 있어도 체크가 된다. 이런 부
분이 true , false 값을 주로 사용하는 개발자 입장에서는 불편하다.


타임리프의 th:checked 는 값이 false 인 경우 checked 속성 자체를 제거한다.


<input type="checkbox" name="active" th:checked="false" />
타임리프 렌더링 후: <input type="checkbox" name="active" />