package hello.hellospring.controller;
import hello.hellospring.domain.Member;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class MemberController {
...
@GetMapping("/members/new")
public String createForm() {
return "members/createMemberForm";
}
@PostMapping("/members/new")
public String create(MemberForm form) {
Member member = new Member();
member.setName(form.getName());
System.out.println("member = " + member.getName());
memberService.join(member);
return "redirect:/";
}
}
(MemberController.java)
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/new" method="post">
<div class="form-group">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을
입력하세요">
</div>
<button type="submit">등록</button>
</form>
</div> <!-- /container -->
</body>
</html>
(resources/templates/members/createMemberForm
)
- 회원 등록 폼
(회원 등록 화면)
2021-08-30 22:36:43.669 INFO 22036 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
member = spring
(등록 후 콘솔 출력 결과)
참조
'Spring' 카테고리의 다른 글
[스프링 입문] H2 데이터베이스 설치 (0) | 2021.08.31 |
---|---|
[스프링 입문] 회원 웹 기능 - 조회 (0) | 2021.08.31 |
[스프링 입문] 회원 웹 기능 - 홈 화면 추가 (0) | 2021.08.30 |
[스프링 입문] 자바 코드로 직접 스프링 빈 등록하기 (0) | 2021.08.30 |
[스프링 입문] 컴포넌트 스캔과 자동 의존관계 설정 (0) | 2021.08.30 |
댓글