2017-06-26 85 views
1

當請求到達控制器方法時,@Valid註釋不會觸發驗證。我該怎麼辦 ?@Valid表單驗證不與Thymeleaf一起使用Spring Boot

父控制器:

public class ApplicationController { 

    @Autowired 
    private I18NService i18NService; 

    public I18NService getI18NService() { 
     return i18NService; 
    } 

    public void setI18NService(I18NService i18NService) { 
     this.i18NService = i18NService; 
    } 
} 

佈局控制器:

@Controller 
public class ClienteController extends ClientLayoutController { 

    @GetMapping("/client/profile") 
    public ModelAndView clientProfile() { 
     ModelAndView mav = this.getModelAndView("profile","client.profile","store/account-profile"); 
     return mav; 
    } 

    @PostMapping("/client/profile") 
    public ModelAndView clientProfileUpdate(@Valid Cliente cliente,BindingResult bindingResult,Model model) { 
     System.out.println("sdfsdf "+cliente.getNome()); 
     System.out.println(bindingResult.getErrorCount()); 

     if(bindingResult.hasErrors()){ 
      ModelAndView mav = this.getModelAndView("profile","client.profile","store/account-profile"); 
      mav.addObject("cliente",cliente); 
      return mav; 
     } 

     return this.getModelAndView("profile","client.profile","store/account-profile"); 
    } 

} 

Thymeleaf形式::

public class ClientLayoutController extends ApplicationController{ 

    @Autowired 
    private UserService userService; 

    @Autowired 
    private ClienteService clienteService; 

    protected ModelAndView getModelAndView(String aActiveMenu, String aI18n, String aViewName){ 
     ModelAndView mav = new ModelAndView(); 
     User user = userService.getAuthenticatedUser(); 
     Cliente cliente = clienteService.getAuthenticatedCliente(); 

     mav.addObject("active_menu",aActiveMenu); 
     mav.addObject("titulo",this.getI18NService().getMessage(aI18n)); 
     mav.addObject("user",user); 
     mav.addObject("cliente",cliente); 
     mav.setViewName(aViewName); 
     return mav; 
    } 
// Getters ans Setters... 
} 

該請求來自於控制器

<form th:method="post" th:action="@{'/client/profile'}" th:object="${cliente}"> 
    <div class="form-group"> 
     <label for="nomecli" th:text="#{client.profile.name}">First Name</label> 
     <input th:field="*{nome}" type="text" id="nomecli" class="form-control" th:placeholder="#{client.profile.name}"/> 
     <span th:if="${#fields.hasErrors('nome')}" th:errors="*{nome}" class="text-danger"></span> 
    </div>    
    <button type="submit" class="btn btn-default btn-theme" th:inline="text"><i class="fa fa-check"></i> [[#{crud.save}]]</button> 
</form> 

實體:

@Entity(name = "cliente") 
public class Cliente { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Column(name = "nome") 
    @NotEmpty(message = "Informe o nome") 
    private String nome; 

// Other fields... 
} 

的bindingResult.getErrorCount()始終爲0,即使我發佈一個空白表單。我曾嘗試在Google上添加@NotNull和其他許多功能,但沒有成功。

回答

1

添加@ModelAttribute("cliente")到控制器的簽名如下:

public ModelAndView clientProfileUpdate(@Valid @ModelAttribute("cliente") Cliente cliente, BindingResult bindingResult,Model model) {...} 

在另一方面,你傳遞一個Entity的觀點,這是用來表示一個數據庫條目。使用數據傳輸對象,它只是一個簡單的POJO類,並將@NotNull@NotEmpty註釋添加到其字段。

+0

它沒有爲我工作,但感謝您的幫助!這與控制繼承無關嗎?因爲我從網上的例子看到的唯一不同的東西就是這個。稍後我會再試一次,讓我有更多時間讓你知道。 – viniciusalvess

+0

我想出了@ balag3,稍後再檢查一下。 – viniciusalvess

相關問題