我的情况类似于以下情况:
public class Shop {
@NotNull
String name;
@NotNull
String desc;
}
用于显示必须在其中插入用户的表单的按钮:
<form:form method="post" action="saveShop.html" modelAttribute="Shop">
<form:label path="name">Name:</form:label>
<form:input path="name" value="${shop.name}" />
<form:label path="desc">desc:</form:label>
<form:input path="desc" value="${shop.desc}" />
<input type="submit" value="Submit"/>
</form:form>
控制器:
@RequestMapping("/addShop")
public ModelAndView LoadFormPage(@ModelAttribute("Shop")Shop shop) {
ModelAndView model = new ModelAndView();
model.setViewName("/shop/addShop");
return model;
}
@RequestMapping(value = "/saveShop", method = RequestMethod.POST)
public ModelAndView saveShop(@Valid @ModelAttribute("Shop") Shop shop, BindingResult result) {
if (result.hasErrors()) { *do some*
} else { *do someelse* }
}
即使我将所有字段都保留为空白(即使使用不同的数据类型,我都尝试过)并提交表格,但控制器永远不会识别错误。
你能帮我吗?
答案 0 :(得分:0)
确保添加以下配置。
<bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
和
<mvc:annotation-driven validator="myBeansValidator">
和
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>