• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

form data

23.11.23 13:13 작성 조회수 201

0

여기서 누르면

여기로 오구

그담에 뷰가 렌더링되는데요

처음에 들어가면 저기 숫자 0이 계속 적혀있어요 ...

아래는 html코드입니다

<!DOCTYPE html>
<html lang="ko"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>form Page</title>
</head>
<body>
<h3>form Page</h3>
<form action="/" th:object="${person}" method="post">
    <div>
        name : <input type="text" th:field="*{name}">
    </div>
    <div>
        age : <input type="text" th:field="*{age}">
    </div>
</form>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

답변 1

답변을 작성해보세요.

0

OMG님의 프로필

OMG

2023.11.24

안녕하세요. jung님, 공식 서포터즈 OMG입니다.

Person클래스가 어떤 구조인지 모르겠으나,

아래와 같이 생성자에 별도로 int 타입의 변수를 초기화하지 않는다면 int의 기본 값은 0으로 초기화 됩니다.

반면 String은 null로 초기화되어 html에서 표기되지 않았습니다.

@Data
@AllArgsConstructor
public class Person {
    private int age;
    private String name;
    public Person () {
        age = 10;
    }
}

 

아래와 같이 한번 출력되는 결과를 확인해보세요 :)

@GetMapping("/form-html")
public String formHtml(Model model) {
    model.addAttribute("person", new Person());

    Person person = new Person();
    System.out.println("person.getAge() = " + person.getAge());
    System.out.println("person.getName() = " + person.getName());
    
    return "form";
}

감사합니다.