3

我在托管 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>

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

Error setting property 'testProp' in bean of type com.ibm.sg.demo.Test: javax.faces.validator.ValidatorException: Stop here, now!

我想:

  • 没有技术部分
  • 见摘要

我想念什么?

4

2 回答 2

3

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

你有三种可能:

  1. 创建您自己的属性解析器
  2. 创建您自己的验证器并将其附加到绑定到托管 bean 的字段
  3. 将验证方法添加到您的 bean

希望这可以帮助

斯文

编辑:

如何向 bean 添加验证方法

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 中。

于 2012-06-20T19:34:15.807 回答
1

一旦您确定您的字段验证失败,您需要做的就是执行此操作,您将得到您想要的:

throw new javax.faces.validator.ValidatorException(new javax.faces.application.FacesMessage("Stop here, now!"));
于 2012-06-20T12:26:38.127 回答