[英]spring mvc javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.String
I try to use the validators for the first time with Spring MVC and I always get the exception: 我尝试在Spring MVC中首次使用验证器,但总是会遇到异常:
javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.String
at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:236)
at org.hibernate.validator.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:219)
at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:167)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:113)
at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:334)
Here is my annoted class for validation: 这是我注释的类,用于验证:
public class AbstractEmployeDto implements Employable, Comparable<AbstractEmployeDto> {
private Set<AbstractClientDto> listeClients = new TreeSet<AbstractClientDto>();
private Long id;
@NotEmpty
private String nom;
@NotEmpty
private String prenom;
@DateTimeFormat(pattern="dd/MM/yyyy")
@Past
private String dateNaissance;
@Size(min=10)
private String telephone;
@Email
private String email;
private TypeEmploye typePersonne;
private String rue;
@Size(min=4, max=4)
private String npa;
getter and setter ...
And the controller with the annotation @valid: 带有注释@valid的控制器:
@RequestMapping(value = "/saveNew", method = RequestMethod.POST)
public ModelAndView saveNewEmploye(@ModelAttribute("command") @Valid AbstractEmployeDto employe,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
if(result.hasErrors()){
return new ModelAndView("/employes/add.html", model);
}
...
@Past
and @DateTimeFormat
must be used on Date
or Calendar
not a String
. @Past
和@DateTimeFormat
必须在Date
或Calendar
上使用,而不是String
。 As bean validation doc says: 正如bean验证文档所述:
The annotated element must be a date in the past. 带注释的元素必须是过去的日期。 Now is defined as the current time according to the virtual machine. 现在根据虚拟机定义为当前时间。 The calendar used if the compared type is of type Calendar is the calendar based on the current timezone and the current locale. 如果比较类型为Calendar,则使用的日历是基于当前时区和当前区域设置的日历。
Supported types are: 支持的类型有:
java.util.Date java.util.Date
java.util.Calendar java.util.Calendar中
null elements are considered valid. 空元素被视为有效。
Porbably you need to use somehting like this: 可能您需要像这样使用:
@DateTimeFormat(pattern="dd/MM/yyyy")
@Past
private Date dateNaissance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.