我的程序似乎不处理表单动作。每次我点击提交按钮时,页面都会重新加载,并且根本没有进入@PostMapping中的函数。替换实际地址没有任何帮助。也许有人知道这与什么有关?
表单的html:
<form action="/register" class="card-back" method="post">
<div class="center-wrap">
<div class="section text-center">
<h4 class="mb-4 pb-3">РЕГИСТРАЦИЯ</h4>
<div class="form-group mt-2">
<input type="email" name="email" class="form-style" placeholder="Почта" id="logemail" autocomplete="off">
<i class="input-icon bi bi-at"></i>
</div>
<div class="form-group mt-2">
<input type="password" name="password" class="form-style" placeholder="Пароль" id="logpass" autocomplete="off">
<i class="input-icon bi bi-lock"></i>
</div>
<button class="btn mt-4" type="submit">submit</button>
</div>
</div>
</form>
控制器:
@Controller
@RequiredArgsConstructor
public class RegController {
private final UsersService usersService;
@Autowired
private UsersRepo usersRepo;
@GetMapping("/register")
public String reg( Model model) {
System.out.println("регистрация");
return "reg";
}
@PostMapping("/register")
public String PostUser (Users users,Model model)
{
System.out.println("зашел");
usersService.createUser(users);
return "redirect:/";
}
}
用户型号:
@Entity
@Getter
@Setter
@Data
@Table(name = "users")
public class Users implements UserDetails {
@Id
@Column(name = "email")
private String email;
@Column (name = "password",length = 1000)
private String password;
@Column (name = "name")
private String name;
@Column(name="phone_number",unique = true)
private String phone_number;
@Column(name = "bDay")
private java.sql.Date bDay;
@Column(name = "active")
private boolean active;
private char sex;
@ElementCollection(targetClass = Role.class,fetch = FetchType.EAGER)
@CollectionTable (name = "user_role",
joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles=new HashSet<>();
public Users()
{}
public Users( String email, String password) {
this.email = email;
this.password = password;
}
}
可以找到这个问题的原因。问题是 spring 安全设置不正确:我没有添加
<input type="hidden" name="_csrf" value="${_csrf.token}">
到 html 登录和注册表单文件。