Jsf 2 之间的差异<;f:事件侦听器="#{};类型=";“事后验证”/&燃气轮机;和普通jsf命令按钮
早上好,我只想知道像这样验证JSF组件的区别:Jsf 2 之间的差异<;f:事件侦听器="#{};类型=";“事后验证”/&燃气轮机;和普通jsf命令按钮,jsf-2,Jsf 2,早上好,我只想知道像这样验证JSF组件的区别: <h:form id="register"> <h:message for="RegisterGroupPanel" style="color:red;" /> <h:panelGrid columns="3" id="RegisterGroupPanel"> <f:event listener="#{user.validatePassword}" type="post
<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>
//这和正常情况有什么区别
在这里,我将操作放在按钮内,并删除了
此
添加了哪些额外功能???当发生给定类型的事件时,
提供了一种调用给定侦听器的方法。postValidate
事件在验证阶段结束时调用,在整个表单处理、转换和验证之后,但在更新模型之前调用。因此,如果您打算根据提交的值执行作业,则需要通过UIInput\getValue()
获取它们
命令按钮的操作方法在调用应用程序阶段调用,在更新模型值阶段之后调用。因此,如果需要提交的值,可以直接访问bean属性
请注意,这两种方法都没有提供在所需组件上自动显示消息的好方法,FacesContext\validationFailed()
在验证失败时将返回true
从这两种方法中,
方法在技术上是执行作业最正确的方法,前提是侦听器方法得到了正确的实现。验证应该在验证阶段执行,而不是在调用应用程序阶段执行
但是,更好的方法是使用专门用于验证多个字段是否相等的组件。JSF实用程序库有这样一个组件:。在您的特定情况下,您可以按如下方式使用它:
<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>
没有任何自定义侦听器方法
<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>