도슐랭스타

Spring - th:field와 th:value 차이 본문

Spring Boot

Spring - th:field와 th:value 차이

도도.__. 2025. 4. 9. 11:35
public class MemberForm {
    private String email = "test@example.com";
    // getter, setter
}
@GetMapping("/register")
public String register(Model model) {
    model.addAttribute("memberForm", new MemberForm());
    return "register-form";
}

이런 상황일 때

th:field일 경우

<form th:object="${memberForm}">
    <input type="text" th:field="*{email}" />
</form>

실제로 브라우저에서 보여지는 HTML (렌더링 결과)

<form>
    <input type="text" name="email" value="test@example.com">
</form>

자동으로 name도 붙고, value도 들어가고, 나중에 form 전송되면 email 값도 넘어감.

th:value일 경우

<form>
    <input type="text" th:value="*{email}" />
</form>

실제로 브라우저에서 보여지는 HTML (렌더링 결과)

<form>
    <input type="text" value="test@example.com">
</form>

값만 들어감. name이 없음 → 나중에 form 전송되면 이 값은 서버로 안 넘어감.

 

반응형

'Spring Boot' 카테고리의 다른 글

Spring - No static resource 에러  (0) 2025.04.09
Comments