2013-05-03 26 views
0

已经存在的问题在JSF 2.1,我需要您的一点帮助。我在我的项目,该项目检查,并提示用户重新输入店名,如果输入的店名在数据库中已存在取得StoreNameValidator。所有这一切都给了我空值。我已经在调试模式下运行它。我给你发送代码。请帮帮我。存储对象为空。请帮帮我。数据使用primefaces

////////////////////////////////////////////// //////////////////////////////////////////////

@FacesValidator("com.kicsit.ehub.validator.StoreNameValidator") 
public class StoreNameValidator implements Validator { 
    @ManagedProperty(value = "#{StoreService}") 
    private IStoreService storeService; 



    @Override 
    public void validate(FacesContext arg0, UIComponent arg1, Object storeName) 
      throws ValidatorException { 
     Store store = null; 
     try{ 
      **store** = getStoreService().getStoreByName(storeName.toString()); 
      if(store != null){ 
       FacesMessage msg = 
         new FacesMessage("Store name Already Registered.", 
           "Already Registered."); 
        msg.setSeverity(FacesMessage.SEVERITY_ERROR); 
        throw new ValidatorException(msg); 
      } 
     } 
     catch(Exception ex){ 

     } 
    } 

    public IStoreService getStoreService() { 
     return storeService; 
    } 

    public void setStoreService(IStoreService storeService) { 
     this.storeService = storeService; 
    } 

} 

,在这里我使用这个验证。

<p:inputText required="true" value="#{storeController.storeBean.storeName}" > 
    <p:ajax event="blur" update="registerMsg" /> 
    **<f:validator validatorId="com.kicsit.ehub.validator.StoreNameValidator" />** 
</p:inputText> 
+0

您目前无法验证或转换器中使用托管属性,你必须使用太多自己查'context.getApplication()evaluateExpressionGet(背景下,elExpression,类型)'。我认为jsf 2.2会支持它。 – djmj 2013-05-03 18:08:25

+0

@djmj u能请详细说明答案? – Umair 2013-05-03 20:31:45

+0

这将有助于http://stackoverflow.com/a/8677938/1530938 @Umair。你得到null是因为没有注入IStoreService(这是因为'FacesConverter'目前不支持注入,JSF2.2承诺会提供这个,这就是djmj所说的有效的 – kolossus 2013-05-03 22:06:05

回答

1

方法定义:

public void validate(FacesContext arg0, UIComponent arg1, Object brandName) 
      throws ValidatorException { 
     Brand brand = null; 
     try{ 
      brand = brandService.getBrandByBrandName(brandName.toString()); 
     } 
     catch(Exception ex){} 
     if(brand != null){ 
      tempBean.setBrandDescription(brand.getBrandDescription()); 
      tempBean.setBrandName(brand.getBrandName()); 
      tempBean.setBrandId(brand.getBrandId()); 
      FacesMessage msg = 
        new FacesMessage("Already Exists", 
          "Re-Enter Brand Name"); 
       msg.setSeverity(FacesMessage.SEVERITY_ERROR); 
       throw new ValidatorException(msg); 
     } 

    } 
XHTML文档中

。用它作为:

<p:outputLabel for="brandNametxt" value="Brand Name :" /> 
        <p:inputText validator="#{brandController.validate}" requiredMessage="Brand Name is Required" id="brandNametxt" required="true" value="#{brandController.bean.brandName}" > 
         <p:ajax event="blur" update="brandNametxt,err-msg,AlreadyExist,:selectedBrand" /> 
        </p:inputText>