2017-11-07 9 views
0

View to send에 대한 유효성 검사 오류를 캡처해야합니다. 나는 몇 가지 튜토리얼 그런 식으로 다음 : 콩 검증 나의 방법 :게시 메서드 스프링 mvc의 오류

@RequestMapping(value = "/novo", method = RequestMethod.POST) 
    public ModelAndView salvar(@Valid Cliente cliente, BindingResult result 
     , RedirectAttributes attributes) { 
    if (result.hasErrors()) { 
     return novo(cliente); 
    } 

    ModelAndView mv = new ModelAndView("redirect:/clientes/novo"); 
    attributes.addFlashAttribute("mensagem", "Cliente salvo com sucesso."); 
    return mv; 
} 

내 클래스 Cliente :

public class Cliente implements EntityModal<Long> { 

private Long id; 

@NotBlank(message = "Nome é obrigatório") 
private String nome; 

@CPF(message = "CPF inválido") 
private String cpf; 

@NotNull(message = "Informe o sexo") 
private Sexo sexo; 

@NotNull(message = "Idade é obrigatória") 
@Min(value = 18, message = "Idade deve ser maior ou igual que 18") 
private Integer idade; 

@Size(max = 50, message = "A observação não pode ter mais que 50 caracteres") 
private String observacao; 
//get and setters... 
} 

오류 :

An Errors/BindingResult argument is expected to be declared immediately 
after the model attribute, the @RequestBody or the @RequestPart arguments to 
which they apply: public org.springframework.web.servlet.ModelAndView 

답변

0

는 모델 객체에 @ModelAttribute를 추가합니다.

@RequestMapping(value = "/novo", method = RequestMethod.POST) 
public ModelAndView salvar(@Valid @ModelAttribute("cliente") Cliente cliente, BindingResult result 
     , RedirectAttributes attributes) { 

} 
+0

속성 모델을 넣어야하기 때문에? 본 튜토리얼에는이 기능이 없습니다. – joeyanthon