반응형
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 {
private final MemberService memberService;
@Autowired
public MemberController(MemberService memberService){
this.memberService = memberService;
}
@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());
memberService.join(member);
return "redirect:/";
}
}
▶ @GetMapping("/members/new") : localhost:8080/members/new에 접속하면 members 폴더에 있는 createMemberForm.html이 화면에 뜸
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>회원가입</title>
</head>
<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>
</body>
</html>
▶ 이름을 입력하고 등록 버튼을 클릭시 name값이 서버로 전송됨
package hello.hellospring.controller;
public class MemberForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
▶ 웹 등록 화면에서 데이터를 전달 받을 폼 객체
▶ localhost:8080/members/new 접속한 모습
반응형
'Spring > inflearn' 카테고리의 다른 글
H2 데이터베이스 설치 (0) | 2022.09.02 |
---|---|
회원 웹 기능 - 조회 (0) | 2022.09.01 |
회원 웹 기능 - 홈 화면 추가 (0) | 2022.08.30 |
자바 코드로 직접 스프링 빈 등록하기 (0) | 2022.08.29 |
컴포넌트 스캔과 자동 의존관계 설정 (0) | 2022.08.28 |