728x90
발생했던 에러
2022-12-27 16:11:38.592 INFO 33716 --- [nio-8088-exec-1] c.l.f.controller.UserController : join controller name =null, passowrd =null
아래의 html을 통해 /user/join으로 userName과 password를 보내려고 했으나 계속 userName과 password가 null로 받아지는 상황.
html
<form action="/users/join" method="post">
<div class="form-field">
<label for="username">User Name</label>
<input type="text" class="form-control" id="username" name="userName">
</div>
<div class="form-field">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<div class="form-field">
<input type="submit" class="btn btn-primary btn-block" value="Register">
</div>
</form>
controller
@RequestMapping("/users")
@Controller
@RequiredArgsConstructor
@Slf4j
public class UserController {
private final UserService userService;
@GetMapping("/join")
public String joinPage(Model model) {
log.info("join controller getmapping");
return "users/register";
}
@PostMapping("/join")
public String join(@ModelAttribute UserJoinRequest request) {
log.info("join controller name ={}, passowrd ={}", request.getUserName(), request.getPassword());
UserDto user = userService.join(request);
return "redirect:/";
}
}
해결
@AllArgsConstructor
@NoArgsConstructor
@Getter @Setter
public class UserJoinRequest {
private String userName;
private String password;
public User toEntity(String password) {
return User.builder()
.userName(this.userName)
.password(password)
.userRole(USER)
.build();
}
}
바인딩을 받아야할 DTO에 Setter가 빠져있었다. Setter를 추가하면 해결된다.
예전에도 발생한 오류였기 때문에 바로 해결할 수 있었다.
반응형