簡體   English   中英

如何在jsf中使用接縫驗證器?

[英]how to use a seam validator in jsf?

我做了一個新的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>

如果要同時檢查,則可以在此處綁定舊的信用卡號。

然后在您的validate方法中:

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>

找到解決方案:)。

忘記標簽庫和東西了!

像這樣使用它:

<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中的任何其他配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM