Код: Выделить всё
@Entity
public class Student {
...
@NotNull
private String name;
@Max(20)
private int age;
< /code>
В старой системе у меня есть Spring's Dispatcher-servlet.xml < /p>
< /code>
В новой системе я использую класс WebConfig вместо < /p>
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
source.setBasename("/WEB-INF/messages");
source.setDefaultEncoding("UTF-8");
return source;
}
Код: Выделить всё
NotNull.studentForm.name = must not be blank
Max.studentForm.age = must not be older than {value}
typeMismatch.studentForm.age = must be numeric
< /code>
В пружинном контроллере < /p>
@Controller
public class StudentMaint {
....
@RequestMapping(value = "/StudentMaintEdit.htm")
public String edit(@ModelAttribute("studentForm") @Valid StudentForm form,
BindingResult result, Map model, HttpServletRequest request) throws Exception {
if (result.hasErrors()) {
form.setMode("show");
return "StudentMaint";
}
....... (no validation coding here)
studentFacade.edit(student); (no try/catch for any exception here)
< /code>
Студенческаяфакада также не имеет никакого кодирования, связанного с валидацией < /p>
in inductionform < /p>
@Component
public class StudentForm {
....
@Valid
private String name;
@Valid
private int age;
< /code>
Единственная разница между старой и новой системой - это использование класса конфигурации вместо XML. Другое кодирование, связанное с валидацией, вообще не имеет изменений. В старой системе сообщения об ошибках проверки правильно устанавливаются контроллером и отображаются через JSP: имя @ExceptionHandler(ConstraintViolationException.class)
public String handleConstraintViolation(ConstraintViolationException ex,
Model model, HttpServletRequest request) {
Set
Подробнее здесь: https://stackoverflow.com/questions/797 ... n-web-tier
Мобильная версия