java - 提交表单时 Spring 枚举异常

标签 java html spring forms enums

我有用户实体和该实体中的字段角色。 角色是 ENUM。我正在尝试从 UI 创建用户。但是,我遇到了一个异常(exception):

org.springframework.beans.NullValueInNestedPathException: Invalid property 'role' of bean class [com.bionic.entities.User]: Could not instantiate property type [com.bionic.entities.Role] to auto-grow nested property path: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.bionic.entities.Role]: Is it an abstract class?; nested exception is java.lang.InstantiationException: com.bionic.entities.Role

这是我的Role.Enum:

package com.bionic.entities;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Resource
public enum Role {
ADMINISTRATOR(1, "administrator"),
TRAINER(2, "trainer"),
STUDENT(3, "student"),
RESTRICTED_ADMINISTRATOR(4, "restricted_administrator"),
RESTRICTED_TRAINER(5, "restricted_trainer");

private long id;
private String name;


Role(){}

private Role(long id, String name) {
    this.name = name;
    this.id = id;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

我的User.class字段:

public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "cell")
private String cell;
@Column(name="position")
private String position;

@Enumerated(EnumType.ORDINAL)
@Column(name = "role_id")
private Role role;

最后是我的html 表单:

<form method="POST" action="/superAdmin/addUser" th:object="${user}">
<select name="role.id" size="2" th:field="*{role.id}" style="display: block" id="role.id"></select>
    <br /> <br /> <input type="submit" value="Upload" class="submit-but">

我花了两天时间才解决这个问题。然而,并没有成功

之后我如何创建实体:

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
public
@ResponseBody
String addUser(@ModelAttribute User user, Model model) {
    try {
        model.addAttribute("user", user);
        superAdministratorService.addUser(user);
        return "successful";
    } catch (Exception e) {
        return "You failed to upload";
    }
}

最佳答案

Role 有一个默认的包级构造函数和一个带有 2 个参数的私有(private)构造函数,请尝试将包级构造函数更改为公共(public),以便执行此操作,更改

Role(){}

public Role(){}

我认为这就是您问题的原因。但是您无法在枚举中设置公共(public)构造函数,因此也许您必须将实现更改为最终类。

更新

public static Role fromId(long id) {
    if (1 == id) {
        return ADMINISTRATOR;
    }
    // TODO else if for the rest of enum instances
    } else {
        throw new AssertionError("Role not know!"); 
    }
}

一个可能的解决方案如下:

  • 使用 DTO(具有与 User 实体以及 getter 和 setter 相同属性的简单 POJO)在 addUser 方法中接收对象,其中 DTO 定义 角色作为整数。
  • 在您的枚举中,创建一个与上面类似的方法
  • 从DTO对象创建实体对象,使用上面的方法在User实体中设置role成员。

关于java - 提交表单时 Spring 枚举异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121432/

相关文章:

java - 在 java.lang.Object 类中使用 protected 方法的目的是什么?

html - 证明内容不在元素之间创建空间

java - Spring Data Rest 中的关联

java - Eclipse Spring IDE 2.3.2 插件与 SpringSource Tool Suite 2.3.2

java - 使用 javax.naming.NameNotFoundException 部署到 Weblogic

java - 具有数百万个节点的有向图,大多数只有几条边,但少数有数十万个

java - Java 反射包装器代码生成器?

java - 到底什么是 CRUD 表

javascript - 隐藏所有表格行,只需要选择一个

css - 如何使div低于另一个div