【问题标题】:show error in one to one mapped field in other tables在其他表中的一对一映射字段中显示错误
【发布时间】:2020-07-11 07:31:14
【问题描述】:

实体类共有三个:User、UserProfile、Country

User 是 OneToOne 映射到 UserProfile UserProfile 是 OneToMany 映射到国家/地区

用户.java

@Entity
@Data
class Users{
   @Id
   private int id;
    @Column(nullable = false)
    @NotBlank
    private String name;
     
     @OneToOne(cascade = CascadeType.ALL)
     @JoinColumn(name = "alumnus_detail_id")
      private UserProfile userProfile;
     
}

下面是 UserProfile.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
 public class UserProfile {
  @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;

  @Past(message="You may be a time traveler..")
  @NotBlank
  private Date dob;
   @ManyToOne( cascade = { CascadeType.MERGE,
        CascadeType.DETACH,CascadeType.REFRESH}  )
   @JoinColumn(name="country_id")
  @NotBlank
   private  Country country;
 }

下面是country.java

@Data
@Getter
@Setter
 @NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="country")
public class Country {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String phonecode;
private String name;

@JsonIgnore
@UpdateTimestamp //hibernate specific feature
private LocalDateTime lastUpdatedDate;

@JsonIgnore
@CreationTimestamp   //hibernate specific feature
private LocalDateTime createdDate;
}

现在,当我想使用 spring:bind 显示国家/地区的验证错误时。显示错误

下面是profile-form.jsp

                    <form:form action="user-process" method="post"  modelAttribute="user">

<spring:bind path="name">
                        <div class="form-group">
                            <label for="name">Name</label>
                            <form:input class="form-control  ${status.error ? 'is-invalid' : ''}"  id="name" path="name"/>
                            <form:errors path="name" cssClass="invalid-feedback" />

                         </div>
                      </spring:bind>
 <spring:bind path="userProfile.country">
                        <div class="form-group">
                            <label for="country">Country:*</label>
                            <form:select class="form-control ${status.error ? 'is-invalid' : ''}" id="country" path="userProfile.country.id">
                                <form:option value="0">Select  </form:option>
                                 <form:options items="${countryList}" itemValue="id" itemLabel="name"/>
                            </form:select>
                            <form:errors path="userProfile.country" cssClass="invalid-feedback" />
                        </div>
  </spring:bind>
  </form>

下面是用户控制器

@RequestMapping("/user-manage")
public String userUpdate(@ModelAttribute(value = "user")  Users user , ModelMap mapData){

     
    mapData.addAttribute("countryList",countryService.findAll());
     
    return "profile-form";
}

但是当包括名称验证在内的其他一切都正常工作时,而不是显示没有验证错误的国家/地区

映射时如何显示验证错误?

【问题讨论】:

    标签: java spring spring-boot jsp


    【解决方案1】:

    在 userProfile 中添加 @Valid 注解。这将激活映射字段的验证

    @OneToOne(cascade = CascadeType.ALL)
     @JoinColumn(name = "alumnus_detail_id")
     @Valid
      private UserProfile userProfile;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多