0

我需要你一点帮助。我在我的项目中创建了 StoreNameValidator,如果输入的商店名称已经存在于数据库中,它会检查并提示用户重新输入商店名称。它一直给我空值。我已经在调试模式下运行它。我给你发代码。请帮我。存储对象为空。请帮帮我。

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

@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>
4

1 回答 1

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>
于 2013-05-29T19:03:17.563 回答