본문 바로가기
스프링과 JPA 기반 웹 어플리케이션 개발/1부 (개발환경, 회원가입, 로그인, 계정설정)

22. 프로필 수정

by Backchus 2020. 4. 21.

프로필 수정 뷰

 

컨트롤러

  • Bio, Link, Occupation, Location 정보만 입력받아서 Account 정보를 수정한다.

SettingsController 추가

package me.weekbelt.studyolle.settings;

@Controller
public class SettingsController {

    @GetMapping("/settings/profile")
    public String profileUpdateForm(@CurrentUser Account account, Model model) {
        model.addAttribute(account);
        model.addAttribute(new Profile(account));
        return "settings/profile";
    }
}

 

화면에 보여줄 정보를 담은 Profile DTO 추가

package me.weekbelt.studyolle.settings;

@NoArgsConstructor
@Data
public class Profile {

    @Length(max = 35)
    private String bio;

    @Length(max = 50)
    private String url;

    @Length(max = 50)
    private String occupation;

    @Length(max = 50)
    private String location;

    public Profile(Account account) {
        this.bio = account.getBio();
        this.url = account.getUrl();
        this.occupation = account.getOccupation();
        this.location = account.getLocation();
    }
}

 

/settins/profile.html 작성

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<!--head-->
<head th:replace="fragments.html::head"></head>

<body class="bg-light">
<!--네비게이션 바-->
<div th:replace="fragments.html::main-nav"></div>

<div class="container">
    <div class="row mt-5 justify-content-center">
        <div class="col-2">
            <div th:replace="fragments.html :: settings-menu(currentMenu='profile')"></div>
        </div>
        <div class="col-8">
            <div th:if="${message}" class="alert alert-info alert-dismissible fade show mt-3" role="alert">
                <span th:text="${message}">메시지</span>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">x</span>
                </button>
            </div>
            <div class="row">
                <h2 class="col-sm-12" th:text="${account.nickname}">weekbelt</h2>
            </div>
            <div class="row mt-3">
                <form action="#" class="col-sm-6"
                th:action="@{/settings/profile}" th:object="${profile}" method="post" novalidate>
                    <div class="form-group">
                        <label for="bio">한 줄 소개</label>
                        <input id="bio" type="text" th:field="*{bio}" class="form-control"
                               placeholder="간략한 소개를 부탁합니다." aria-describedby="bioHelp" required>
                        <small id="bioHelp" class="form-text text-muted">
                            길지 않게 35자 이내로 입력하세요.
                        </small>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('bio')}" th:errors="*{bio}">
                            조금 길어요.
                        </small>
                    </div>
                    <div class="form-group">
                        <label for="url">링크</label>
                        <input id="url" type="text" th:field="*{url}" class="form-control"
                               placeholder="http://studyolle.com" aria-describedby="urlHelp" required>
                        <small id="urlHelp" class="form-text text-muted">
                            블로그, 유튜브 또는 포트폴리오나 좋아하는 웹 사이트 등 본인을 표현할 수 있는 링크를 추가하세요.
                        </small>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('url')}" th:errors="*{url}">
                            올바른 URL이 아닙니다. 예시처럼 입력해 주세요.
                        </small>
                    </div>
                    <div class="form-group">
                        <label for="occupation">직업</label>
                        <input id="occupation" type="text" th:field="*{occupation}" class="form-control"
                               placeholder="어떤 일을 하고 계신가요?" aria-describedby="occupationHelp" required>
                        <small id="occupationHelp" class="form-text text-muted">
                            개발자? 매니저? 취준생? 대표님?
                        </small>
                    </div>
                    <div class="form-group">
                        <label for="location">활동 지역</label>
                        <input id="location" type="text" th:field="*{location}" class="form-control"
                               placeholder="Redmond, WA USA" aria-describedby="locationHelp" required>
                        <small id="locationHelp" class="form-text text-muted">
                            주요 활동(사는 곳이나 직장을 다니는 곳 또는 놀러 다니는 곳) 지역의 도시 이름을 알려주세요.
                        </small>
                    </div>

                    <div class="form-group">
                        <button class="btn btn-primary btn-block" type="submit" aria-describedby="submitHelp">수정하기</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- footer -->
    <div th:replace="fragments.html::footer"></div>
</div>
</body>
</html>

 

 

참고: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-JPA-%EC%9B%B9%EC%95%B1#

 

스프링과 JPA 기반 웹 애플리케이션 개발 - 인프런

이 강좌에서 여러분은 실제로 운영 중인 서비스를 스프링, JPA 그리고 타임리프를 비롯한 여러 자바 기반의 여러 오픈 소스 기술을 사용하여 웹 애플리케이션을 개발하는 과정을 학습할 수 있습니다. 이 강좌를 충분히 학습한다면 여러분 만의 웹 서비스를 만들거나 취직에 도움이 될만한 포트폴리오를 만들 수 있을 겁니다. 활용 웹 개발 프레임워크 및 라이브러리 Java Spring Spring Boot Spring Data JPA Thymeleaf 온라인 강의 스

www.inflearn.com