2015-11-11 59 views
0

我在驗證工作,數據綁定和類型轉換...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; 
+0

嘗試提交表單沒有輸入值的位置字段,然後檢查它。 – developerbhuwan

+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"; 
    } 

    // ... 
} 

只留下modelAttributeBindingResult爲你的方法輸入參數。 編輯:

@RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST) 
    private String saveAccount ( @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand, 
            BindingResult result) { 

     return "registerAccountView"; 

    } 
+0

增加了@Valid,結果相同:-( –

+0

hm將@Valid添加到''用戶'字段的'DataAccountCommand'類中 '@Valid User userBean' –

+0

添加了相同的結果: - ( –