在提交表单期间,如果存在任何验证错误,则表单在字段下方显示错误消息。但是实际的问题在另一个地方。我有两个实体 User 和 UserDetails 。这些实体通过双向一对一映射相互映射。验证仅适用于 User 实体字段,而不适用于 UserDetails 实体。
春季-5.0.2 ` 休眠-5.2.10
User.java
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotEmpty(message="{common.error.msg}")
private String first_name;
@NotEmpty(message="{common.error.msg}")
private String last_name;
@NotEmpty(message="{common.error.msg}")
private String status;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private UserDetails userDetails;
//Getter and setter methods
}
UserDetails.java
@Entity
@Table(name="user_details")
public class UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int user_id;
@NotEmpty(message="{common.error.msg}")
private String address;
@NotEmpty(message="{common.error.msg}")
private String mobile;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id")
private User user;
//Getter and setter methods
}
控制器类中的Get和Post方法
@GetMapping(value="/create")
public String loadUserForm(Model model) {
model.addAttribute("command", new User());
return "backend/oms/user/form"; //JSP
}
@PostMapping(value="/create")
public String Save(@Valid @ModelAttribute("command") User user, BindingResult br, Model model, HttpSession session, RedirectAttributes ra) {
if(br.hasErrors()) {
return "backend/oms/user/form"; //JSP
} else {
try {
int id = userService.save(user);
if(id > 0) {
ra.addFlashAttribute("flash_msg", "ok|User added!");
return "redirect:/oms/user/edit/"+id;
} else {
return "backend/oms/user/create"; //JSP
}
} catch (ConstraintViolationException ex) {
model.addAttribute("err", "Something wrong! Please try again.");
return "backend/oms/user/form"; //JSP
}
}
}
messages.properties
common.error.msg=This field is required!
form.jsp
<form:form action="/oms/user/create" method="post" modelAttribute="command">
<label>First Name</label>
<form:input path="first_name" class="form-control" placeholder="First Name" value="" />
<form:errors cssClass="error" path="first_name" />
<label>Last Name</label>
<form:input path="last_name" class="form-control" placeholder="Last Name" value="" />
<form:errors cssClass="error" path="last_name" />
<label>Mobile</label>
<form:input path="userDetails.mobile" class="form-control" placeholder="Mobile" value="" />
<form:errors cssClass="error" path="userDetails.mobile" />
<label>Address</label>
<form:textarea path="UserDetails.address" class="form-control" placeholder="Address" value="" />
<form:errors cssClass="error" path="userDetails.address" />
<label>Status</label>
<form:select class="form-control" path="status">
<option value="">Choose...</option>
<option value="E">Enable</option>
<option value="D">Disable</option>
</form:select>
<form:errors path="status" cssClass="error"/>
<input type="submit" class="btn btn-primary" value="Save" />
</form>
Please see the screenshot 如您所见,仅来自User实体的字段正在验证,而来自UserDetails的字段正在验证。请帮忙。
答案 0 :(得分:0)
现在解决了。 @有效注释解决了我的问题。我需要在实体变量声明之前放置@Valid批注,如下所示-
@OneToMany(mappedBy="majorHead", cascade = CascadeType.ALL)
@Valid
private List<MinorHead> minorHead = new ArrayList<MinorHead>();