Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation @有效表单验证不适用于Thymeleaf Spring启动_Validation_Spring Boot_Thymeleaf - Fatal编程技术网

Validation @有效表单验证不适用于Thymeleaf Spring启动

Validation @有效表单验证不适用于Thymeleaf Spring启动,validation,spring-boot,thymeleaf,Validation,Spring Boot,Thymeleaf,当post请求到达控制器方法时,@Valid注释不会触发验证。我该怎么办 父控制器: public class ApplicationController { @Autowired private I18NService i18NService; public I18NService getI18NService() { return i18NService; } public void setI18NService(I18N

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

父控制器:

    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...
}
布局控制器:

    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
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>
即使我发布了一个空白表单,bindingResult.getErrorCount()也始终为0。我曾尝试在google上添加@NotNull和许多其他内容,但没有成功。

@modeldattribute(“客户”)
添加到控制器的签名中,如下所示:

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

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

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


这对我不起作用,但谢谢你的帮助!这不是和controlelr继承有关吗?因为我在网上看到的唯一不同的例子就是这个。等我有时间再试试,然后告诉你。我在@balag3中发现了这个问题,请稍后再查看。