我在驗證工作,數據綁定和類型轉換...BindingResult不是在春天3.2.8工作在Spring 3.2.8使用JSR-303 Bean驗證
我有這個控制器
@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()是假的!!!!!
我已經試過也在使用另一種方法:
@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;
嘗試提交表單沒有輸入值的位置字段,然後檢查它。 – developerbhuwan
這就是我所做的:-( –