and normal jsf command button 之间的差异-6ren"> and normal jsf command button 之间的差异-早上好,我只想知道像这样验证 JSF 组件之间的区别: // diff between that and normal -6ren">
gpt4 book ai didi

jsf-2 - and normal jsf command button 之间的差异

转载 作者:行者123 更新时间:2023-12-04 05:04:15 27 4
gpt4 key购买 nike

早上好,我只想知道像这样验证 JSF 组件之间的区别:

<h:form id="register">

<h:message for="RegisterGroupPanel" style="color:red;" />

<h:panelGrid columns="3" id="RegisterGroupPanel">

<f:event listener="#{user.validatePassword}" type="postValidate" /> // diff between that and normal <h:command>

<h:outputLabel for="username" value="Username : " />
<h:inputText id="username" value="#{user.username}" required="true"
requiredMessage="Please enter username" />
<h:message for="username" style="color: red;" />


<h:outputLabel for="password" value="Password : " />
<h:inputSecret id="password" value="#{user.password}" required="true"
requiredMessage="Please enter password" />
<h:message for="password" style="color: red;" />


<h:outputLabel for="confirmPassword" value="Confirm password : " />
<h:inputSecret id="confirmPassword" required="true"
requiredMessage="Please enter confirm password" />
<h:message for="confirmPassword" style="color: red;" />

</h:panelGrid>

<h:commandButton action="thanks" value="register" />

</h:form>

在这里,我将操作放在按钮内并删除了 <f:event listener="#{user.validatePassword}" type="postValidate" />
<h:form id="register">

<h:message for="RegisterGroupPanel" style="color:red;" />

<h:panelGrid columns="3" id="RegisterGroupPanel">


<h:outputLabel for="username" value="Username : " />
<h:inputText id="username" value="#{user.username}" required="true"
requiredMessage="Please enter username" />
<h:message for="username" style="color: red;" />


<h:outputLabel for="password" value="Password : " />
<h:inputSecret id="password" value="#{user.password}" required="true"
requiredMessage="Please enter password" />
<h:message for="password" style="color: red;" />


<h:outputLabel for="confirmPassword" value="Confirm password : " />
<h:inputSecret id="confirmPassword" required="true"
requiredMessage="Please enter confirm password" />
<h:message for="confirmPassword" style="color: red;" />

</h:panelGrid>

<h:commandButton action="#{user.validatePassword}" value="register" />

</h:form>

这有什么额外的功能 <f:event>添加??

最佳答案

<f:event>提供了一种调用给定 listener 的方法给定事件的方法 type发生。 postValidate事件在验证阶段结束时被调用,在整个表单被处理、转换和验证之后,但 之前 模型已更新。因此,如果您打算根据提交的值执行作业,则需要通过 UIInput#getValue() 获取它们。 .

在更新模型值阶段之后,在调用应用程序阶段调用命令按钮的操作方法。因此,如果您需要提交的值,则可以直接访问 bean 属性。

请注意,这两种方法都没有提供在所需组件处自动显示消息的好方法,也没有提供 FacesContext#validationFailed() 的方法。会返回 true在验证失败的情况下。

从两个方面来说,<f:event type="postValidate">如果监听器方法被正确实现,方法在技术上是执行工作的最正确的方法。验证应该在验证阶段而不是在调用应用程序阶段执行。

但是,更好的方法是使用专门用于验证多个字段是否相等的组件。 JSF 实用程序库 OmniFaces有这样一个组件: <o:validateEqual> .在您的特定情况下,您可以按如下方式使用它:

<h:form id="register">
<h:panelGrid columns="3" id="RegisterGroupPanel">
<h:outputLabel for="username" value="Username : " />
<h:inputText id="username" value="#{user.username}" required="true"
requiredMessage="Please enter username" />
<h:message for="username" style="color: red;" />

<h:outputLabel for="password" value="Password : " />
<h:inputSecret id="password" value="#{user.password}" required="true"
requiredMessage="Please enter password" />
<h:panelGroup>
<h:message for="password" style="color: red;" />
<h:message for="validateConfirm" style="color:red;" />
</h:panelGroup>

<h:outputLabel for="confirmPassword" value="Confirm password : " />
<h:inputSecret id="confirmPassword" required="true"
requiredMessage="Please enter confirm password" />
<h:message for="confirmPassword" style="color: red;" />
</h:panelGrid>

<o:validateEqual id="validateConfirm" components="password confirmPassword" message="Passwords are not equal" />
<h:commandButton action="thanks" value="register" />
</h:form>

没有任何自定义监听器方法。

关于jsf-2 - <f :event listener ="#{}" type ="postValidate"/> and normal jsf command button 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15727933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com