我的英语不好,抱歉。我的问题是关于 jsf 验证。当我的验证 bean 抛出 ValidatorException 时,我的托管 bean save_method 有效。但我不希望我的 save_method 有效。如果电子邮件无效,我的 save_method 有效不应该工作。
复合接口(interface)
<composite:attribute name="validator" required="false"
method-signature="void Action(javax.faces.context.FacesContext, javax.faces.component.UIComponent,Object)" />
像这样的复合实现
<c:if test="#{cc.getValueExpression('validator')!=null}">
<p:inputText value="#{cc.attrs.value}" id="#{cc.attrs.id}"
required="#{cc.attrs.required}" validator="#{cc.attrs.validator}"/>
验证验证 bean 中的方法。
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String email = (String) value;
Pattern mask = null;
mask = Pattern.compile(EMAIL_REGEX);
Matcher matcher = mask.matcher(email);
if (!matcher.matches()) {
messageManager.warn(MessageManager.Bundles.WO, "validator.message.email");
throw new ValidatorException(new FacesMessage("hoppala"));
}
}
以及我的托管 Bean 保存方法。
@Override
public boolean save() {
super.save();
}
最佳答案
您使用的 mojarra 版本低于 2.1.10 吗?有was a bug最近得到修复。因此,如果可以的话,请更新到最新版本 2.1.13。但无论如何,在 JSF 中您不会按照您所描述的方式进行操作。我建议使用 <f:validator/>
应用 validator ,像这样:
<mytags:customInput value="#{validationModel.value}">
<f:validator for="input" validatorId="org.validationexample.EmailValidator" />
</mytags:customInput>
您的复合组件必须实现 <cc:editableValueHolder>
:
<cc:interface>
<cc:attribute name="value"/>
<cc:editableValueHolder name="input" targets="inputText"/>
</cc:interface>
<cc:implementation>
<p:inputText id="inputText" value="#{cc.attrs.value}" />
</cc:implementation>
最后,您的验证由专用 validator 类处理:
@FacesValidator("org.validationexample.EmailValidator")
public class EmailValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
// your validation here
}
}
无论如何,还有其他方法/风格。您可以放置<f:validator>
也可以直接放到你的复合组件里面<p:inputText>
。自 <f:validator>
有一个disabled
属性,您可以根据需要禁用它。另一种方法是使用 Bean Validation 并采用 Hibernate Validator 附带的现有 EmailValidator 。在这种情况下,您只需在属性上方设置注释@Email。
关于java - 使用复合组件进行 Jsf 验证无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12436855/