[英]how to use a seam validator in jsf?
I have made a new Seam validator: 我做了一个新的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);
}
}
}
The problem is that I do not know how to use it directly from jsf... 问题是我不知道如何直接从jsf使用它...
The following does not work.... 以下无效。
I have declared it in a special taglib file: myvalidators.taglib.xml 我已经在特殊的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>
and tried to use it like: 并尝试像这样使用它:
<h:inputText id="someField" value="#{booking.creditCardName}"
required="true" label="County">
<my:roCountyValidator/>
<h:message for="someField"/>
</h:inputText>
Can you tell me where I am wrong? 你能告诉我我哪里错了吗?
Thanks. 谢谢。
Two ways to solve this. 有两种解决方法。
One, is to use as @BalusC has written. 一种是使用@BalusC编写的。 You don't need to define anything in faces-config.xml
您无需在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>
Here you can even bind the old credit card number, if you want to check that also. 如果要同时检查,则可以在此处绑定旧的信用卡号。
Then in your validate method: 然后在您的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")));
}
}
The other way, is to use the validator
tag in <h:inputText>
另一种方法是在
<h:inputText>
使用validator
标签
You don't even need to create a @Validator
class, as long as it's a seam component and if you use the same method signature. 只要它是一个接缝组件并且使用相同的方法签名,甚至不需要创建
@Validator
类。
I use a validator component for all my general validators 我对所有常规验证器都使用了验证器组件
@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");
}
}
}
Now you can add the validator: 现在,您可以添加验证器:
<h:inputText value="#{foo.bar}" required="true" validator="#{validator.positiveInteger}">
<s:validate/>
<h:inputText>
I have no idea about the Seam part, it might have different approaches for this, but in standard JSF, you normally define it as <validator>
in faces-config.xml
. 我对Seam部分一无所知,它可能有不同的方法,但是在标准JSF中, 通常在
faces-config.xml
中将其定义为<validator>
。
<validator>
<validator-id>roCountyValidator</validator-id>
<validator-class>validators.RoCountyValidator</validator-class>
</validator>
and use it as follows: 并如下使用它:
<h:inputText>
<f:validator validatorId="roCountyValidator" />
</h:inputText>
Solution found:). 找到解决方案:)。
Forget about taglibs and stuff! 忘记标签库和东西了!
Use it like: 像这样使用它:
<h:inputText id="someField" value="#{booking.creditCardName}"
required="true" label="County" validator="roCountyValidator">
<h:message for="someField"/>
</h:inputText>
Please remark that 请说明
validator="roCountyValidator"
it shouldn't be used like EL expression !!! 它不应该像EL表达式一样使用! (my first wrong decision)
(我的第一个错误决定)
So the advantage of using Seam + @Validator: Seam will transform that component in the background to a jsf validator so you do no longer need jsf validator tags or any other configuration in faces-config.xml. 因此,使用Seam + @Validator的好处是:Seam会将后台的该组件转换为jsf验证器,因此您不再需要jsf验证器标签或faces-config.xml中的任何其他配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.