Я новичок в весне, и я пытаюсь зарегистрировать пользователя, используя страницу thymeleaf. Когда я нажимаю кнопку отправки, модель (registrationForm), полученная контроллером, является нулевой.
Когда я нажимаю кнопку «Отправить», вызывается метод PostRegistration. Когда я проверил атрибут в объекте модели, все поля были нулевыми. Я попытался посмотреть в Интернете, но не смог найти решение. Буду признателен за любую помощь.
Сообщение об ошибке
2020-04-18 16:56:14.500 TRACE 48956 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : POST "/register", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2020-04-18 16:56:14.500 TRACE 48956 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to tacos.security.RegistrationController#ProcessRegistration(RegistrationForm, Errors, SessionStatus)
2020-04-18 16:56:14.502 TRACE 48956 --- [nio-8080-exec-1] o.s.w.m.support.InvocableHandlerMethod : Arguments: []
2020-04-18 16:56:14.529 TRACE 48956 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: [RegistrationForm(username=null, password=null, confirmpassword=null, fullname=null, street=null, city=null, state=null, zip=null, phone=null), org.springframework.validation.BeanPropertyBindingResult: 9 errors
Ниже моя страница тимьяна html.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Taco Cloud</title>
<link rel="stylesheet" th:href="@{/styles.css}" />
</head>
<body>
<h1>Register</h1>
<input type="hidden" name="_csrf" th:value="${_csrf.token}" />
<img th:src="@{/images/TacoCloud.png}" />
<br/>
<form method="POST" th:action="@{/register}" th:object="${registrationForm}" >
<div th:if="${#fields.hasErrors()}">
<br/>
<span class="validationError"> Please correct the problems
below and resubmit. </span>
</div>
<label for="username">Username: </label>
<input type="text" th.field="*{username}" />
<span class="validationError" th:if="${#fields.hasErrors('username')}"
th:errors="*{username}">username cannot be blank</span>
<br />
<label for="password">Password: </label>
<input type="password" th.field="*{password}" />
<br />
<label for="confirmPassword">Confirm password: </label>
<input type="password" th.field="*{confirmpassword}" />
<br />
<label for="fullname">Full name: </label>
<input type="text" th.field="*{fullname}" />
<br />
<label for="street">Street: </label>
<input type="text" th.field="*{street}" />
<br />
<label for="city">City: </label>
<input type="text" th.field="*{city}" />
<br />
<label for="state">State: </label>
<input type="text" th.field="*{state}" />
<br />
<label for="zip">Zip: </label>
<input type="text" th.field="*{zip}" />
<br />
<label for="phone">Phone: </label>
<input type="text" th.field="*{phone}" />
<br />
<input type="submit" value="Register" />
</form>
</body>
</html>
Мой класс RegistrationContoller
import javax.validation.Valid;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;
import lombok.extern.slf4j.Slf4j;
import tacos.data.UserRepository;
@Controller
@RequestMapping("/register")
@Slf4j
public class RegistrationController {
private UserRepository userRepo;
private PasswordEncoder passwordEncoder;
public RegistrationController(UserRepository userRepo, PasswordEncoder passwordEncoder) {
this.userRepo = userRepo;
this.passwordEncoder = passwordEncoder;
}
@ModelAttribute("registrationForm")
public RegistrationForm registrationForm() {
return new RegistrationForm();
}
@GetMapping
public String sendRegistrationForm() {
return "registration";
}
@PostMapping
public String ProcessRegistration(@Valid RegistrationForm registrationForm, Errors errors, SessionStatus sessionStatus) {
log.info("password " + registrationForm.getPassword());
log.info("Confirm password " + registrationForm.getConfirmpassword());
if (errors.hasErrors()) {
System.out.println("Inside Errors");
return "registration";
}
userRepo.save(registrationForm.toUser(passwordEncoder));
return "redirect:/login";
}
}
Моя регистрационная форма. java
package tacos.security;
import javax.validation.constraints.NotBlank;
import org.springframework.security.crypto.password.PasswordEncoder;
import lombok.Data;
import tacos.User;
@Data
public class RegistrationForm {
@NotBlank(message = "Name is required")
private String username;
@NotBlank(message = "password cannot be blank")
private String password;
@NotBlank(message = "Please confirm password")
private String confirmpassword;
@NotBlank(message = "Name is required")
private String fullname;
@NotBlank(message ="stree is required")
private String street;
@NotBlank(message ="city is required")
private String city;
@NotBlank(message ="state is required")
private String state;
@NotBlank(message ="zip is required")
private String zip;
@NotBlank(message ="phone is required")
private String phone;
public User toUser(PasswordEncoder passwordEncoder) {
return new User(username, passwordEncoder.encode(password), fullname, street, city, state,zip,phone);
}
}