如何在JSF中显示应用程序的错误? [英] How to display my application's errors in JSF?
问题描述
在我的JSF / Facelets应用程序中,以下是我表单部分的简化版本:
In my JSF/Facelets app, here's a simplified version of part of my form:
<h:form id="myform">
<h:inputSecret value="#{createNewPassword.newPassword1}" id="newPassword1" />
<h:message class="error" for="newPassword1" />
<h:inputSecret value="#{createNewPassword.newPassword2}" id="newPassword2" />
<h:message class="error" for="newPassword2" />
<h:commandButton value="Continue" action="#{createNewPassword.continueButton}" />
</h:form>
我希望能够根据某些特定的h:message标签分配错误在continueButton()方法中发生。需要为newPassword和newPassword2显示不同的错误。验证器将无法正常工作,因为将会传递结果(来自DB)的方法在continueButton()方法中运行,并且运行两次太贵了。
I'd like to be able to assign an error to a specific h:message tag based on something happening in the continueButton() method. Different errors need to be displayed for newPassword and newPassword2. A validator won't really work, because the method that will deliver results (from the DB) is run in the continueButton() method, and is too expensive to run twice.
我无法使用h:messages标签,因为该页面有多个地方需要显示不同的错误消息。当我尝试这个,页面显示每个消息的重复。
I can't use the h:messages tag because the page has multiple places that I need to display different error messages. When I tried this, the page displayed duplicates of every message.
我尝试这是一个最好的猜测,但没有运气:
I tried this as a best guess, but no luck:
public Navigation continueButton() {
...
expensiveMethod();
if(...) {
FacesContext.getCurrentInstance().addMessage("newPassword", new FacesMessage("Error: Your password is NOT strong enough."));
}
}
我缺少什么?任何帮助将不胜感激!
What am I missing? Any help would be appreciated!
推荐答案
如果有人好奇,我能够根据您的所有回答组合!
In case anyone was curious, I was able to figure this out based on all of your responses combined!
这是在Facelet中:
This is in the Facelet:
<h:form id="myform">
<h:inputSecret value="#{createNewPassword.newPassword1}" id="newPassword1" />
<h:message class="error" for="newPassword1" id="newPassword1Error" />
<h:inputSecret value="#{createNewPassword.newPassword2}" id="newPassword2" />
<h:message class="error" for="newPassword2" id="newPassword2Error" />
<h:commandButton value="Continue" action="#{createNewPassword.continueButton}" />
</h:form>
这是在continueButton()方法中:
This is in the continueButton() method:
FacesContext.getCurrentInstance().addMessage("myForm:newPassword1", new FacesMessage(PASSWORDS_DONT_MATCH, PASSWORDS_DONT_MATCH));
它的工作原理!感谢您的帮助!
And it works! Thanks for the help!
这篇关于如何在JSF中显示应用程序的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!