如何在jsf中使用seam验证器?

我做了一个新的Seam验证器:
    package validators;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Validator;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

@Name("roCountyValidator")
@Validator
@BypassInterceptors
public class RoCountyValidator implements javax.faces.validator.Validator,
        Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3876319398131645955L;
    Log log = Logging.getLog(RoCountyValidator.class);

    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        log.info("validating....!");
        if (String.valueOf(value).equals("Arad"))
            ((UIInput) component).setValid(true);
        else {
            ((UIInput) component).setValid(false);
            FacesMessage message = new FacesMessage();
            message.setDetail("Invalid county");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }
}
问题是我不知道如何直接从jsf使用它... 以下不起作用.... 我在一个特殊的taglib文件中声明了它:myvalidators.taglib.xml
<facelet-taglib>
<namespace>http://example.com/jsf/my/validators</namespace>
<tag>
    <tag-name>roCountyValidator</tag-name>
    <validator>
        <validator-id>roCountyValidator</validator-id>
    </validator>
</tag>
并尝试使用它像:
<h:inputText id="someField" value="#{booking.creditCardName}" 
                               required="true" label="County">
                <my:roCountyValidator/>
                <h:message for="someField"/>
            </h:inputText>
你能告诉我哪里错了吗? 谢谢。     
已邀请:
解决这个问题的两种方法。 一,是用@BalusC写的。 您不需要在faces-config.xml中定义任何内容
<h:inputText id="cc" required="true" value="#{booking.creditCardName}">
                <f:validator validatorId="roCountyValidator"/>
                <f:attribute name="oldCreditCardNumber" value="#{booking.creditCardName}" />
                <s:validate />
</h:inputText>
在这里,您甚至可以绑定旧的信用卡号码,如果您也想检查它。 然后在您的验证方法中:
public void validate(FacesContext context, UIComponent component,
        Object value) throws ValidatorException {
    log.info("validating....!");

    String oldCreditcard = String.valueOf(component.getAttributes().get("oldCreditCardNumber"));
    String newCreditCard = (String) value;
    if(SomeClass.isCorrectCreditcard(newCreditCard)) {
        //You don't need to setValid(false), this is done automatically
        Map<String, String> messages = Messages.instance();
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, messages.get("wrongCreditCardNumber"), messages
                    .get("wrongCreditCardNumber")));

    }
}
另一种方法是使用
<h:inputText>
中的
validator
标签 你甚至不需要创建一个
@Validator
类,只要它是一个接缝组件并且你使用相同的方法签名。 我为所有通用验证器使用验证器组件
@Name("validator")
@Scope(ScopeType.EVENT)
@BypassInterceptors
public class Validator {

public void positiveInteger(FacesContext context, UIComponent toValidate, Object value) {
        String val = (String) value;

        try {
            int v = Integer.parseInt(val);
            if (v < 0)
                throw new NumberFormatException();
        } catch (NumberFormatException e) {
            ((UIInput) toValidate).setValid(false);
            FacesMessages.instance().addToControlFromResourceBundle(toValidate.getId(), "invalid.integer");
        }
    }
}
现在您可以添加验证器:
<h:inputText value="#{foo.bar}" required="true" validator="#{validator.positiveInteger}">
  <s:validate/>
<h:inputText>
    
我不知道Seam部分,它可能有不同的方法,但在标准的JSF中,你通常在
faces-config.xml
中将其定义为
<validator>
<validator>
    <validator-id>roCountyValidator</validator-id>
    <validator-class>validators.RoCountyValidator</validator-class>
</validator>
并按如下方式使用:
<h:inputText>
    <f:validator validatorId="roCountyValidator" />
</h:inputText>
    
找到解决方案:)。 忘记taglibs和东西吧! 使用它像:
<h:inputText id="someField" value="#{booking.creditCardName}" 
                               required="true" label="County" validator="roCountyValidator">
                <h:message for="someField"/>
            </h:inputText>
请注意
validator="roCountyValidator"
它不应该像EL表达式一样使用!!! (我的第一个错误决定) 因此使用Seam + @Validator的优势:Seam会将后台的组件转换为jsf验证器,因此您不再需要jsf验证器标签或faces-config.xml中的任何其他配置。     

要回复问题请先登录注册