gpt4 book ai didi

java - 如何在 jsf 中使用接缝 validator ?

转载 作者:行者123 更新时间:2023-11-29 06:16:45 25 4
gpt4 key购买 nike

我做了一个新的 Seam validator :

    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>

在这里你甚至可以绑定(bind)旧的信用卡号,如果你也想查看的话。

然后在你的验证方法中:

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")));

}
}



另一种方法是使用 validator<h:inputText> 中标记

您甚至不需要创建 @Validator类,只要它是接缝组件并且使用相同的方法签名即可。

我为所有通用 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");
}
}
}

现在你可以添加 validator 了:

<h:inputText value="#{foo.bar}" required="true" validator="#{validator.positiveInteger}">
<s:validate/>
<h:inputText>

关于java - 如何在 jsf 中使用接缝 validator ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com