java - 如何将对象从注册表传输到 Controller 以写入数据库并分配角色?

标签 java spring spring-boot spring-mvc

我有一个通用的 User 类及其具有特定角色的后代。如何根据注册表中的选择在注册 Controller 中创建对象?这可以通过单选按钮和百里香叶来完成吗?

@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public class User implements UserDetails, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;

    @Transient
    private String matchingPassword;
    private String name;
    private String surname;
    private String patronymic;
    private LocalDate birthday;
    private String hometown;
    private String number;
    private String mail;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private List<Role> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return getRoles();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@NoArgsConstructor
@Entity(name = "Student")
@Table(name = "student", schema = "public")
public class Student extends User {

    private String studentIdNumber;
    private String groupNumber;
    private String academicPerformance;
    private String faculty;
    private String department;
    private long rating;

    @Builder
    public Student(Long id,
                   String username,
                   String password,
                   String matchingPassword,
                   String name,
                   String surname,
                   String patronymic,
                   LocalDate birthday,
                   String hometown,
                   String number,
                   String mail,
                   String studentIdNumber,
                   String groupNumber,
                   String academicPerformance,
                   String faculty,
                   String department,
                   List<Role> roles) {
        super(id, username, password, matchingPassword, name, patronymic, surname, birthday, hometown, number, mail, roles);
        this.studentIdNumber = studentIdNumber;
        this.groupNumber = groupNumber;
        this.academicPerformance = academicPerformance;
        this.faculty = faculty;
        this.department = department;
        this.rating = 0;
    }
}

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@NoArgsConstructor
@Entity(name = "Director")
@Table(name = "director", schema = "public")
public class Director extends User {

    private String department;

    @Builder
    public Director(Long id,
                    String username,
                    String password,
                    String matchingPassword,
                    String name,
                    String surname,
                    String patronymic,
                    LocalDate birthday,
                    String hometown,
                    String number,
                    String mail,
                    String department,
                    List<Role> roles) {
        super(id,
                username,
                password,
                matchingPassword,
                name,
                surname,
                patronymic,
                birthday,
                hometown,
                number,
                mail,
                roles);
        this.department = department;
    }
}

在其中创建对象以写入数据库并定义角色的 Controller :

    @GetMapping("/registration")
    public String showRegistrationForm(Model model) {
        log.info("showRegistrationForm method called");
        model.addAttribute("newUser", new User());   <----- Creation of a specific object. How!?
        model.addAttribute("avatar", "default_avatar.png");
        model.addAttribute("username", "Unknown");

        return "registration";
    }

最佳答案

我刚刚添加了用户选择角色的映射。

关于java - 如何将对象从注册表传输到 Controller 以写入数据库并分配角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61178808/

相关文章:

java - 将多个单词写入一个 CSV 单元格

java - 尝试使用 @Retryable 排除异常 - 导致抛出 ExhaustedRetryException

java - 如何使用 Spring 的 RestTemplate 来 POST 一个字符串数组

java - 如何在与 Spring Boot 的集成测试中 Autowiring 存储库?

java - Spring Boot 与 @JsonIgnore

java - 在spring rest multipart image upload api中获取BAD REQUEST

java - 显示 0 的行数

spring-boot - 使用 Spring 重命名 API

java - struts.xml 标签结果类型的基于注释的配置替代方案

java - Spring REST API 和国际化