인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

박시균님의 프로필 이미지

작성한 질문수

실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발

@modelattribute , setter관련 질문

작성

·

2.2K

0

혼자 개인 프로젝트를 진행하는데 궁금증이 생겨 질문드립니다.

@modelattribute를 사용해서 바인딩하려면 dto에 setter가 구현이 되어있어야 하는걸로 알고있습니다.

그런데 혼자 프로젝트를 끄적이다가 한dto는 setter를 구현안했음에도 바인딩이 되길래 궁금증이 생겨 질문드려요!

 

 

--------fundingDto를 입력받는 폼------------

 

<form class="validation-form" novalidate th:action th:object="${fundingDto}" method="post" enctype="multipart/form-data">

                <div class="mb-3">
                    <label for="restaurantName">가게이름</label>
                    <input type="text" class="form-control" id="restaurantName" placeholder="restaurantName" th:field="*{restaurantName}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="menu">메뉴</label>
                    <input type="text" class="form-control" id="menu" placeholder="menu" th:field="*{menu}" th:errorclass="field-error" required>

                </div>

                <div class="mb-3">
                    <label for="information">가게정보</label>
                    <input type="text" class="form-control" id="information" placeholder="information" th:field="*{information}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="introduction">가게소개</label>
                    <input type="text" class="form-control" id="introduction" placeholder="introduction" th:field="*{introduction}" th:errorclass="field-error" required>

                </div>

                <div class="mb-3">
                    <label for="notice">가게 공지사항</label>
                    <input type="text" class="form-control" id="notice" placeholder="notice" th:field="*{notice}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="discountPrice">할인가격</label>
                    <input type="text" class="form-control" id="discountPrice" placeholder="discountPrice" th:field="*{discountPrice}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="price">가격</label>
                    <input type="text" class="form-control" id="price" placeholder="price" th:field="*{price}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="minFundingCount">최소펀딩수</label>
                    <input type="text" class="form-control" id="minFundingCount" placeholder="minFundingCount" th:field="*{minFundingCount}" th:errorclass="field-error" required>

                </div>

                <div class="mb-3">
                    <label for="maxFundingCount">최대펀딩수</label>
                    <input type="text" class="form-control" id="maxFundingCount" placeholder="maxFundingCount" th:field="*{maxFundingCount}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="startDate">펀딩 시작 날짜</label>
                    <input type="datetime-local" class="form-control" id="startDate" placeholder="startDate" th:field="*{startDate}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="endDate">펀딩 종료 날짜</label>
                    <input type="datetime-local" class="form-control" id="endDate" placeholder="endDate" th:field="*{endDate}" th:errorclass="field-error" required>

                </div>

                <div class="mb-3">
                    <label for="mainImage">메인 이미지</label>
                    <input type="file" class="form-control" id="mainImage" placeholder="mainImage" multiple="multiple" th:field="*{mainImage}" th:errorclass="field-error" required>
                </div>

                <div class="mb-3">
                    <label for="imageFiles">이미지</label>
                    <input type="file" class="form-control" id="imageFiles" placeholder="imageFiles" multiple="multiple" th:field="*{imageFiles}" th:errorclass="field-error" required>
                </div>

                <button class="btn btn-primary btn-lg btn-block" type="submit">펀딩 등록</button>
            </form>

 

-------------------------------------------------------------------------

fundingDto

 

@Getter
@Builder
@AllArgsConstructor
public class FundingDto {

    private Long id;
    private String restaurantName;
    private String menu;
    private String information;
    private String introduction;
    private String notice;
    private Integer discountPrice;
    private Integer price;
    private Integer minFundingCount;
    private Integer maxFundingCount;
    private Integer nowFundingCount;
    private MultipartFile mainImage;
    private List<MultipartFile> imageFiles;
    private List<Image> imageList;

    @Enumerated(EnumType.STRING)
    private FundingStatus fundingStatus;

    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
    private LocalDateTime startDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
    private LocalDateTime endDate;

    public void setMainImage(MultipartFile mainImage) {
        this.mainImage = mainImage;
    }
}

 

다른 dto는 @setter를 달아주지않으면 null이 바인딩 되는데 이 객체는 왜 바인딩이 성공하는지 잘 모르겠어요..

그리고 한가지 더 질문이 있는데 @modelattribute 를 사용함에 있어 @Setter를 사용하지 않고는 사용이 불가능한가요?

@Setter를 되도록 사용하지 않는게 좋다고 들어가지고 질문드립니다. 

 

답변 1

0

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. 박시균님

modelattribute Setter로 구글에 검색해보시면 원하시는 답을 찾으실 수 있을거에요^^

https://minchul-son.tistory.com/546

감사합니다. 

박시균님의 프로필 이미지
박시균
질문자

감사합니다!! 도움됐어요!