Validation 引发验证程序异常时显示错误详细信息

Validation 引发验证程序异常时显示错误详细信息,validation,jsf,xpages,Validation,Jsf,Xpages,我在托管bean中有代码: public void setTestProp(String newProp) { FacesMessage yourFailure = new FacesMessage(); yourFailure.setDetail("Really you need to promise to never do that again!"); yourFailure.setSummary("Stop here, now!"); yourFailure.set

我在托管bean中有代码:

public void setTestProp(String newProp) {
   FacesMessage yourFailure = new FacesMessage();
   yourFailure.setDetail("Really you need to promise to never do that again!");
   yourFailure.setSummary("Stop here, now!");
   yourFailure.setSeverity(FacesMessage.SEVERITY_FATAL);
   throw new ValidatorException(yourFailure);
}
在XPage中:

<xp:messages id="messages1" layout="table" showSummary="false"
    showDetail="true" globalOnly="false">
</xp:messages>

但我得到了结果消息(正如预期的那样,很好地位于黄色框中,而不是错误页面中):

在com.ibm.sg.demo.Test类型的bean中设置属性“testProp”时出错:javax.faces.validator.validator异常:立即停止

我想:

  • 没有技术部分
  • 见摘要

我错过了什么?

一旦确定字段验证失败,您需要做的就是这样做,您将得到您想要的:

throw new javax.faces.validator.ValidatorException(new javax.faces.application.FacesMessage("Stop here, now!"));

问题是属性解析器从托管bean的get/set方法捕获所有java.lang.Throwable。“原始”facesMessage将替换为一条新消息(附加上一条消息)

你有三种可能:

  • 创建自己的属性解析器
  • 创建您自己的验证器并将其附加到绑定到托管bean的字段
  • 向bean中添加验证方法
  • 希望这有帮助

    斯文

    编辑:

    如何向bean中添加验证方法

    public void validate(FacesContext context, UIComponent toValidate,  Object value){
    
        // Do your validation with value
        // if everything is ok, exit method
    
        // if not, flag component invalid...
        ((UIInput)toValidate).setValid(false);
    
        // ... create your message ...
        FacesMessage yourFailure = new FacesMessage();
        yourFailure.setDetail("Really you need to promise to never do that again!");
        yourFailure.setSummary("Stop here, now!");
        yourFailure.setSeverity(FacesMessage.SEVERITY_FATAL);
    
        context.addMessage(toValidate.getClientId(context),  yourFailure);
    }
    
    a) 向bean中添加验证方法

    public void validate(FacesContext context, UIComponent toValidate,  Object value){
    
        // Do your validation with value
        // if everything is ok, exit method
    
        // if not, flag component invalid...
        ((UIInput)toValidate).setValid(false);
    
        // ... create your message ...
        FacesMessage yourFailure = new FacesMessage();
        yourFailure.setDetail("Really you need to promise to never do that again!");
        yourFailure.setSummary("Stop here, now!");
        yourFailure.setSeverity(FacesMessage.SEVERITY_FATAL);
    
        context.addMessage(toValidate.getClientId(context),  yourFailure);
    }
    
    b) 将验证器添加到字段中

    <xp:inputText id="inputText1"
    value="#{TBean.test}"
    validator="#{TBean.validate}">
    
    
    
    (您可以根据需要为方法命名。)


    这个验证器没有添加到faces config.xml中。

    Jeremy->这就是我的代码所做的,只是在更多的行中。错误框被正确触发,但细节都没有显示(只有摘要),前面有“bean”的部分也没有。似乎它覆盖了总结,有点像。所以问题仍然存在open@stwissel,您的代码在更新模型阶段被执行,甚至在页面加载之前就已经有一个项目绑定到该字段,我的假设是Jeremy的代码在页面提交后的调用应用程序或验证阶段发生。你的意思是每次加载都会失败吗?@stwissel是正确的-我没有注意到你在setter中这样做。不要那样做。您希望使用验证器进行验证,因此它会在流程验证阶段执行。不久前我在XPagesBlog上写了一篇文章——你可以在这里阅读:感谢你的回答!托比:它只有在提交时才失败(在设置事件中)。杰里米:那就是我借用代码的地方。我想我需要在豆子里加点东西。。。返回drawing board.thx以获取详细信息。我可以向bean添加一个验证函数,没问题。有没有一种方法可以在提交时自动调用该函数,或者我需要处理onSubmit事件(没有数据源就不存在)或者按钮中的代码?有没有一种方法可以在bean中自动调用验证方法?或者我需要在onSubmit事件中调用验证(如果没有数据源,它是不存在的)。此外,自定义验证器需要位于faces-config.xml中。或者我可以通过编程方式附加它吗?该方法称为“自动”。不需要自定义验证器。我在上面的回答中添加了一个例子。太酷了。Thx很多。这正是我想要的。我假设我可以使用this.validators并拥有多个函数callNo,您不能使用validators属性,因为EL是不允许的。该属性仅用于注册的验证器(实现验证器接口并添加到faces config.xml的类)。