代码之家  ›  专栏  ›  技术社区  ›  Malin

来自验证程序类的消息不出现在xpage的消息控件中?

  •  3
  • Malin  · 技术社区  · 6 年前

    我已经创建了一个类来测试person对象。例如

    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将被重新路由。

    我怎样才能防止这种情况?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Stanislaw Guzik    6 年前

    我已经复制了你的代码片段和来自 valIdNumber inputText 组件到 viewScope 变量而不是托管bean字段,并删除 disabled 如果 输入文本 validateRequired 将显示的消息。 println 服务器控制台中的输出作为方法至少被激发的证据?

    更新:

    这是因为在你的情况下 InvokeApplication

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

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

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

    另一个解决方案是 ValidatorException 相反,当您的检查失败时,它会自动将组件设置为无效,并告诉生命周期管理过程跳转到 RenderResponse 阶段,避免 UpdateModel 调用应用程序 验证程序异常 FacesContext :

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