我正在使用Spring 3.2.8中的验证,数据绑定和类型转换...
我有这个控制器
@Controller
public class SaveAccountController {
@Autowired
private UserService userService;
@RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
private String saveAccount ( @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand,
BindingResult result) {
return "registerAccountView";
}
}
这个java对象:
public class DataAccountCommand {
@Valid
User userBean;
public User getUserBean() {
return userBean;
}
public void setUserBean(User userBean) {
this.userBean = userBean;
}
@Override
public String toString() {
return "DataAccountCommand [userBean=" + userBean + "]";
}
}
和另一个:
@SuppressWarnings("serial")
@Entity
@Table(name = "T_USER", uniqueConstraints = { @UniqueConstraint(columnNames = "LOGIN"), @UniqueConstraint(columnNames = "EMAIL") })
@SequenceGenerator(name = "seqUSER", sequenceName = "SEQ_USER")
public class User implements java.io.Serializable {
@Id
@Column(name = "ID", unique = true, nullable = false, precision = 38, scale = 0)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqUSER")
private Long id;
@Column(name = "ROLE", nullable = false, precision = 38, scale = 0)
@Enumerated(EnumType.ORDINAL)
protected UserRole userRole;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "COMPANY")
private Company company;
@Column(name = "POSITION")
@NotNull
@Size(min=10)
private String position;
....
}
,这在applicationContext.xml
中<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
这是我的servlet:
<!-- Maps incoming URLs to classnames -->
<bean name="controllerHandler" class="springext.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="basePackage" value="fr.telecom.controller" />
<property name="interceptors">
<list>
<!-- Checks a specific request parameter if the locale is changed. -->
<ref bean="localeChangeInterceptor"/>
<ref bean="sessionInterceptor"/>
</list>
</property>
</bean>
<!-- Action URL mappings to controllers. -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<!-- Checks a specific request parameter if the locale is changed. -->
<ref bean="localeChangeInterceptor"/>
<ref bean="sessionInterceptor"/>
</list>
</property>
</bean>
控制器位置的为空,但result.hasErrors()为false !!!!!
我也试过用另一种方法:
@Controller
public class SaveAccountController {
@Autowired
private UserService userService;
@Autowired
LocalValidatorFactoryBean validator;
@RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
private String saveAccount ( @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand,
Errors errors) {
validator.validate(dataAccountCommand, errors);
}
}
但后来又出现了另一个奇怪的错误:
java.lang.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.getProviderUtil()Ljavax/persistence/spi/ProviderUtil;
答案 0 :(得分:1)
我没看到您验证@ModelAttribute("dataAccountCommand")
的位置。在某处添加验证逻辑。
请参阅their参考文档。您有两种选择:
第一个是自定义验证器:
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "petForm";
}
// ...
}
第二个是@Valid
注释:
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@Valid @ModelAttribute("pet") Pet pet, BindingResult result) {
if (result.hasErrors()) {
return "petForm";
}
// ...
}
仅保留modelAttribute
和BindingResult
作为方法的输入参数。
编辑:
@RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
private String saveAccount ( @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand,
BindingResult result) {
return "registerAccountView";
}