Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc springmvc&;JSR303验证导致错误400_Spring Mvc_Bean Validation_Hibernate Validator - Fatal编程技术网

Spring mvc springmvc&;JSR303验证导致错误400

Spring mvc springmvc&;JSR303验证导致错误400,spring-mvc,bean-validation,hibernate-validator,Spring Mvc,Bean Validation,Hibernate Validator,嗯。我有一个Spring MVC控制器方法,如下所示: public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, Model model ,BindingResult result) public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam

嗯。我有一个Spring MVC控制器方法,如下所示:

public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, Model model ,BindingResult result)
public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, BindingResult result, Model model)

GeoInfoParam类定义有许多带有@NotNull、@Size注释的验证的输入,我希望在提交表单时执行这些输入(就像我在ModelAttribute中使用@Valid一样)。但是,当我单击submit时,页面显示了400个错误,并且没有在日志中显示任何其他信息。

我调试了执行流,发现org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.java(这是HandlerAdapter执行的一部分).HandleBindeException在没有BindingResult后跟ModelAttribute时调用,此方法使用400代码设置响应。 我学到的经验是,如果您有一个用@Valid注释的表单(modeldattribute),请确保它后面紧跟着bindingsult。 因此,我已将我的控制器方法签名更改如下:

public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, Model model ,BindingResult result)
public String getInformationByGID(@ModelAttribute("geoInfoParam") @Valid GeoInfoParam geoInfoParam, BindingResult result, Model model)
我真傻。现在一切都好了。希望这对别人有帮助


注意:我使用Spring MVC 3.2.8和Hibernate validator 4.2.0 final

谢谢!你救了我从桥上跳下来!英雄联盟但是现在,我想知道如何一次验证两个或多个
ModelAtributes
(我得到了需要持久化两个实体的情况)。