【问题标题】:@Valid form validation not working with Thymeleaf Spring Boot@Valid 表单验证不适用于 Thymeleaf Spring Boot
【发布时间】:2017-11-28 20:45:44
【问题描述】:

@Valid 注解在 post 请求到达控制器方法时不会触发验证。我该怎么办?

父控制器:

    public class ApplicationController {

    @Autowired
    private I18NService i18NService;

    public I18NService getI18NService() {
        return i18NService;
    }

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

布局控制器:

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...
}

请求进来的Controller:

@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");
    }

}

百里香形式:

<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 和许多其他内容,但没有成功。

【问题讨论】:

    标签: validation spring-boot thymeleaf


    【解决方案1】:

    删除了我在应用程序上拥有的所有自定义验证器,并删除了我在其他控制器上拥有的所有 @InitBinders 以自定义验证它们。现在 @Valid 具有预期的行为。 在我自己发现之后,下面的帖子解释得更好。

    Spring - mixing annotation and validator-based validations

    【讨论】:

      【解决方案2】:

      @ModelAttribute("cliente") 添加到控制器的签名中,如下所示:

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

      另一方面,您将Entity 传递给视图,用于表示数据库条目。使用数据传输对象,它只是一个简单的 POJO 类,并在其字段中添加 @NotNull@NotEmpty 注释。

      【讨论】:

      • 它对我不起作用,但感谢您的帮助!它与控制器继承无关吗?因为我从网上的例子中看到的唯一不同的是这个。等我有时间再试试,然后告诉你。
      • 我知道@balag3,稍后再检查。
      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2021-01-17
      • 1970-01-01
      相关资源
      最近更新 更多