JSF 2 - 复合组件 - 由于具有空值的组件,Bean验证失败

时间:2013-01-16 09:51:55

标签: forms jsf-2 bean-validation composite-component

我有一个复合组件(实际上是验证码),我将其用于h:form 当我点击我的h:表单的提交按钮时出现以下错误。

  16 panv。 2013 10:02:06 javax.faces.validator.BeanValidator验证
  注意:无法验证具有空值的组件:captcha:captchaText

以下是验证码的复合组件

的xhtml代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
  xmlns:composite="http://java.sun.com/jsf/composite" xmlns:xcc="http://java.sun.com/jsf/composite/components">
<h:body>

  <composite:interface>
    <composite:attribute name="id" type="java.lang.String" />
    <composite:attribute name="imageWidth" type="java.lang.Integer" />
    <composite:attribute name="imageHeight" type="java.lang.Integer" />
    <composite:attribute name="imageClass" type="java.lang.String" />
    <composite:attribute name="inputClass" type="java.lang.String" default="" />
    <composite:attribute name="placeholder" type="java.lang.String" default="" />
    <composite:attribute name="failureMessage" type="java.lang.String" default="Captcha validation failed" />

    <composite:editableValueHolder name="captchaText" />
  </composite:interface>

  <composite:implementation>
    <h:inputText id="captchaText" required="true" styleClass="#{cc.attrs.inputClass}" placeholder="#{cc.attrs.placeholder}"
      autocomplete="off" maxlength="10">
      <f:param name="failureMessage" value="#{cc.attrs.failureMessage}" />
      <f:validateLength minimum="6" />
      <f:validator validatorId="captchaValidator" />
    </h:inputText>
    <h:graphicImage id="captchaImage" alt="" style="margin-left:3px;" styleClass="#{cc.attrs.imageClass}" width="#{cc.attrs.imageWidth}"
      height="#{cc.attrs.imageHeight}" value="__captcha.jpg" />
  </composite:implementation>

</h:body>
</html>

这是Captcha的JSF Validator代码。

import javax.faces.application.FacesMessage;


import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.paissad.waqtsalat.servlet.CaptchaServlet;

    @FacesValidator(CaptchaValidator.VALIDATOR_ID)
    public class CaptchaValidator implements Validator {

    public static final String VALIDATOR_ID = "captchaValidator";

    @Override
    public void validate(final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {

        final String enteredValue = (String) value;
        final String expectedValue = (String) context.getExternalContext().getSessionMap()
            .get(CaptchaServlet.CAPTCHA_KEY);

        final boolean ok = (enteredValue != null) && (enteredValue.equals(expectedValue));
        if (!ok) {
            String summary = (String) component.getAttributes().get("failureMessage");
             if (summary == null) summary = "";
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, summary));
        }
    }

}

我不是真的有必要展示构建真实验证码图像的CaptchaServlet的代码。我只能说它确实有效。我从这段代码中感受到了灵感here

这里基本上是我在表单中使用复合组件的方式。 (这不是我真正的用例btw)

<h:form>
    <xcc:captcha id="captcha" imageClass="img-rounded" placeholder="#{i18n.Captcha_enter_code_here}" failureMessage="Captcha validation failed" />
    <h:commandButton styleClass="btn btn-success" type="submit" value="#{i18n.Submit}"
            actionListener="#{accountRegistrationController.register}" action="success" />
</h:form>

表单呈现方式如下(带法语本地化):

enter image description here

我更喜欢使用复合组件+验证器+ servlet
我更喜欢避免使用控制器bean(@Named / @RequestScoped)和验证Captcha输入值的方法。

当我在Eclipse中调试Validator代码时,代码行为正常。唯一错误的是,以下部分代码永远不会从Captcha的组件中检索“failureMessage”属性,我该如何解决这个问题呢?它始终为空。

String summary = (String) component.getAttributes().get("failureMessage");

那么,我如何解决Bean验证问题呢? 如何在Validator中正确检索属性'failureMessage?

我尝试了很多东西,现在我依靠你 在此先感谢您的帮助。

PS:我目前正在使用TomEE 1.5 +,Java6。

1 个答案:

答案 0 :(得分:1)

看起来您的inputText标记缺少value属性。 value属性执行输入和bean之间的绑定。

<h:inputText value="#{yourBean.yourValue" ...
相关问题