在我的应用程序中我有控制器:
package org.sample.web.config;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.sample.model.Contact;
import org.sample.model.Person;
import org.sample.model.PersonGender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import ch.ralscha.extdirectspring.generator.IncludeValidation;
import ch.ralscha.extdirectspring.generator.ModelGenerator;
import ch.ralscha.extdirectspring.generator.OutputFormat;
@Controller
@RequestMapping
public class ModelController {
private final Logger logger = LoggerFactory.getLogger(getClass());
@InitBinder
public void initBinder(WebDataBinder binder) {
logger.error("aaaaaaaaaaaaaaa");
binder.registerCustomEditor(PersonGender.class, new EnumPropertyEditor(PersonGender.class));
}
@RequestMapping("/app/model/Person.js")
public void user(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelGenerator.writeModel(request, response, Person.class, OutputFormat.EXTJS4, IncludeValidation.ALL, true);
}
@RequestMapping("/app/model/Contact.js")
public void catalog(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelGenerator.writeModel(request, response, Contact.class, OutputFormat.EXTJS4, IncludeValidation.ALL, true);
}
}
问题是带有注释InitBinder的方法永远不会调用,所以当我想验证我的enut时它会引发异常:
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: org.sample.model.PersonGender.
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.verifyResolveWasUnique(ConstraintValidatorManager.java:218)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.findMatchingValidatorClass(ConstraintValidatorManager.java:193)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:97)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:125)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:91)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:478)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:424)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:388)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:340)
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:158)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102)
at org.springframework.validation.DataBinder.validate(DataBinder.java:772)
编辑:
PersonGender:
package org.sample.model;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public enum PersonGender implements Serializable {
MALE("M"), FEMALE("F"), UNKNOWN("0");
private static final long serialVersionUID = 1L;
private static Map<String, PersonGender> identifierMap = new HashMap<String, PersonGender>();
static {
for (PersonGender value : PersonGender.values()) {
identifierMap.put(value.getValue(), value);
}
}
private String value;
private PersonGender(String value) {
this.value = value;
}
public static PersonGender fromValue(String value) {
PersonGender result = identifierMap.get(value);
if (result == null) {
throw new IllegalArgumentException("No PersonGender for value: " + value);
}
return result;
}
public String getValue() {
return value;
}
public String getName() {
return name();
}
}
提交表格时会发生异常:
@Service
public class SimpleService {
...
@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "person")
public ExtDirectFormPostResult validatePersonForm(@Valid Person p, BindingResult result) {
if (!result.hasErrors()) {
// another validations
}
return new ExtDirectFormPostResult(result);
}
...
}