处理表示层中的 JSF 验证错误,避免 h:message



有没有办法让JSF对我的输入进行验证,但让我控制如何显示验证错误?我想避免使用h:message

当我在需要验证支持的表单中的每个输入组件下面编写一个h:message组件时,我发现这会使我的代码过于混乱。由于页面上有多个表单,因此使用h:messages不是一个选项,因为这也会显示其他表单的错误消息。所以我想处理验证后发送到表示层的错误消息,并使用 JS/Jquery 自己完成错误表示任务。那么如何处理 JSF 验证服务抛出的错误呢?

只是根据UIForm#isSubmitted()有条件地渲染<h:messages>

<h:form binding="#{form1}">
    <h:messages rendered="#{form1.submitted}" />
    ...
</h:form>
<h:form binding="#{form2}">
    <h:messages rendered="#{form2.submitted}" />
    ...
</h:form>
<h:form binding="#{form3}">
    <h:messages rendered="#{form3.submitted}" />
    ...
</h:form>

或者只是引入一些 ajax 魔法并仅更新当前表单而不是整个视图。

<h:form>
    <h:messages />
    ...
    <h:commandButton ...><f:ajax execute="@form" render="@form" /></h:commandButton>
</h:form>
<h:form>
    <h:messages />
    ...
    <h:commandButton ...><f:ajax execute="@form" render="@form" /></h:commandButton>
</h:form>
<h:form>
    <h:messages />
    ...
    <h:commandButton ...><f:ajax execute="@form" render="@form" /></h:commandButton>
</h:form>

所以我想处理验证后发送到表示层的错误消息,并使用 JS/Jquery 自己完成错误表示任务。

创建自定义组件或渲染器来替换<h:messages>

最新更新