【问题标题】:message from validator class does not appear in messages control on xpage?来自验证器类的消息没有出现在 xpage 的消息控件中?
【发布时间】:2018-08-20 13:32:08
【问题描述】:

我创建了一个类来测试一个人对象。例如

public class PersonValidators implements Serializable {

    private static final long serialVersionUID = 1L;

    public PersonValidators(){      
    }

    public void valIdNumber(FacesContext facesContext, UIComponent component, Object value) {          
        System.out.println(this.getClass().getSimpleName().toString() + " - valId(...), value=" + value.toString());
        String msg = "Invalid id for person provided";
        FacesContext.getCurrentInstance().addMessage(null, new javax.faces.application.FacesMessage(javax.faces.application.FacesMessage.SEVERITY_INFO, msg, ""));
   }
 }

并且我已注册为托管 bean,如下所示:

<managed-bean>
<managed-bean-name>personValidators</managed-bean-name>
<managed-bean-class>com.mybank.mycard.test.PersonValidators</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
</managed-bean>

对于我的控件,我已定义使用该方法作为验证器:

<xp:inputText id="idNumber"
    value="#{personBean.person.idNumber}"
    disabled="#{!personBean.person.editable}"
    validator="#{personValidators.valIdNumber}" required="true">
    <xp:this.validators>
        <xp:validateRequired
            message="Field PassNumber is empty">
        </xp:validateRequired>
    </xp:this.validators>
</xp:inputText>

在 xpage 我添加了一个消息控件:

<xp:panel
    rendered="#{javascript:facesContext.getMessages().hasNext()}"
    style="margin-top:15px;">
    <div class="row">
        <div class="col-md-12">
            <xp:panel>
                <xp:this.styleClass><![CDATA[#{javascript:return "alert alert-danger"}]]></xp:this.styleClass>
                <xp:messages id="msgBox"></xp:messages>
            </xp:panel>
        </div>
    </div>
</xp:panel>

在我设置的 xsp.properties 中:

xsp.client.validation=false

问题是来自我的验证器类的消息永远不会出现在消息控件中。

我忽略了什么?

更新:

我没有贴出初始化验证的代码:

<xp:button value="Save" id="SaveButton"
    styleClass="btn-primary" rendered="#{personBean.person.editable}">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.action>
            <xp:actionGroup>
                <xp:confirm
                    message="Are you sure you want to submit this case?" />
                <xp:executeScript>
                    <xp:this.script><![CDATA[#{javascript:personBean.save();
            facesContext.getExternalContext().redirect("person.xsp?custId=" + personBean.getPerson().getCustomer().getCustId())}]]></xp:this.script>
                </xp:executeScript>
            </xp:actionGroup>
        </xp:this.action>
    </xp:eventHandler>
</xp:button>

当我删除 facesContext... 时会显示消息。如果我添加它,xpages 将被重新路由。

如何防止这种情况发生?

我虽然在将消息添加到 facescontext 时 XPage 不会继续下一阶段:-?

【问题讨论】:

    标签: java xpages


    【解决方案1】:

    我已经复制了您的代码 sn-ps 并且来自valIdNumber 的消息已成功显示在消息控件中。我将 inputText 组件的值设置为 viewScope 变量而不是托管 bean 字段并删除了 disabled 属性的唯一区别,但这无关紧要。 如果inputText 没有输入值,则该方法不会触发,在这种情况下,只会显示validateRequired 的消息。 您是否总是将服务器控制台中的 println 输出视为该方法至少被触发的证据?

    更新:

    发生这种情况是因为在您的情况下,即使您的验证器失败,您的重定向(和保存!)完成的InvokeApplication 阶段仍然会执行。您应该明确告诉引擎您不想继续。

    在返回之前将以下几行添加到您的验证方法中:

    UIInput myInput = (UIInput)component;
    myInput.setValid(false);
    

    这些行应该在 FacesContext.addMessage 执行的同一条件块中执行,即验证失败时。

    另一种解决方案是抛出ValidatorException,而不是当您的检查失败时,它会自动将组件设置为无效并告诉生命周期管理进程跳转到RenderResponse阶段,避免UpdateModelInvokeApplication阶段。您也可以在ValidatorException 构造函数中提供您的自定义消息,它将自动添加到FacesContext

    throw new javax.faces.validator.ValidatorException(
        new FacesMessage("Invalid id for person provided")
    );
    

    【讨论】:

    • 嗨 Stanislaw,我用触发验证的代码更新了我的示例。当我不包含 facescontext 重定向时,该消息会出现在消息控件中。但在我的提交按钮下,我想添加一些有条件的重新路由。我该怎么做?
    • 我已根据您的评论对我的答案添加了更新
    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2022-10-13
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多