我有一个用户类型为admin,client和Supplier的枚举类。我在用户模型类中用@Enumerated(EnumType.STRING)注释了我的userType属性。但是,运行代码时出现类型不匹配的错误。

我正在使用带有varchar的spring boot和mysql数据库作为Enum usertype字段。我下面的代码有更多详细信息。

我的枚举班

UserType.java

   public enum UserType {

     CLIENT ("Client"),
     SUPPLIER ("Supplier"),
     ADMIN ("Admin");

     private final String type;

     UserType (String userType){

       this.type = userType;

     }

     public String getType() {

       return this.type;
     }
   }


User.java

UserType utype的代码段。

@Entity
public class User{

 @Enumerated(EnumType.STRING)
 @Column (name = "user_type", nullable = false)
 private UserType utype;

 @Enumerated(EnumType.STRING)
 public UserType getUtype() {

   return utype;
 }

 @Enumerated(EnumType.STRING)
 public void setUtype(UserType utype) {
   this.utype = utype;
 }
}


控制者

   @GetMapping(value="/newUser")
   public String registrationForm(Model model){
     model.addAttribute("user", new User());
     model.addAttribute("UserTypes", UserType.values());

     return "register";
   }

  @PostMapping(value="/registerUser")
  public String registerUser(@ModelAttribute(value = "user") User user){

        userService.save(user);
        return "pages/login";
   }


百里香叶查看文件

register.html

     <select th:field="*{utype}" >
      <option th:each="usertype : ${UserTypes}"
             th:text="${usertype.type}"
     th:value="utype">
    </option>
    </select>


我希望将utype的输入转换为字符串,但出现以下错误。

字段“ utype”上对象“用户”中的字段错误:拒绝的值[utype];代码[typeMismatch.user.utype,typeMismatch.utype,typeMismatch.com.grocery.demo.Model.UserType,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[user.utype,utype];参数[];默认消息[utype]];默认消息[无法将类型'java.lang.String'的属性值转换为属性'utype'的必需类型'com.grocery.demo.Model.UserType';嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型[java.lang.String]转换为类型[@ javax.persistence.Enumerated @ javax.persistence.Column com.grocery.demo.Model.UserType]为值'utype';嵌套异常是java.lang.IllegalArgumentException:没有枚举常量com.grocery.demo.Model.UserType.utype]]

使用application.properties配置mysql

spring.datasource.url=jdbc:mysql://localhost:3306/FredSystems
spring.datasource.username=root
spring.datasource.password=fred@2017
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create

spring.jpa.show-sql=true
spring.messages.basename=validation


 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

最佳答案

做到如下简单:

控制者

 @GetMapping(value="/newUser")
   public String registrationForm(Model model){
     model.addAttribute("user", new User());
     model.addAttribute("UserTypes",Arrays.asList(UserType.values()));
     return "register";
   }


在您的百里香叶中:

<select th:field="*{utype}" >
      <option th:each="usertype : ${UserTypes}"
             th:text="${usertype}"
     th:value="${usertype}">
    </option>
    </select>


如果是JSP:

<select id="editState"class="form-control">
                        <c:forEach items="${UserTypes}" var="ut" varStatus="loop">
                            <option value="${ut}">${ut}</option>
                        </c:forEach>
                    </select>

10-04 11:15