我在托管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!
我想:
我想念什么?
答案 0 :(得分:3)
问题是属性解析器从托管bean的get / set方法捕获所有 java.lang.Throwable 。 “原始” facesMessage 将替换为新的(前一条消息被附加)。
您有三种可能性:
希望这有帮助
斯文
修改强>
如何向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。
答案 1 :(得分:1)
一旦确定您的字段验证失败,您只需执行此操作,即可获得所需内容:
throw new javax.faces.validator.ValidatorException(new javax.faces.application.FacesMessage("Stop here, now!"));