我添加了一些代码来验证 javafaces 站点中的字段:
<h:outputLabel value="Password:"></h:outputLabel>
<h:inputSecret id="password" binding="#{password}" value="#{register.password}" required="true" requiredMessage="Password field must be filled in"></h:inputSecret>
<h:outputLabel value="Repeat password:"></h:outputLabel>
<h:inputSecret id="password2" required="true" requiredMessage="Repeat password field must be filled in">
<f:validator validatorId="sameValueValidator"/>
<f:attribute name="value" value="#{password.value}"/>
<f:attribute name="message" value="Passwords do not match"/>
</h:inputSecret>
第一次提交表单时,正确的属性值被传递给 validator 。以下时间虽然该值与第一个值相同,但无论用户在字段中键入什么。知道如何重置它吗?
最佳答案
假设您的 validator 已正确实现,代码看起来不错并且应该可以正常工作。获取先前提交的第一个密码字段的值的症状只应在您将该字段保留为空时出现。那是因为你正在传递 #{password.value}
当验证失败时,它将包含以前的值。您应该改为传递 #{password.submittedValue}
, 但那又会包含 null
验证成功时。
如果您在字段中输入其他内容时确实获得了先前的值,那么这可能是由于您在问题中显示的内容以外的其他内容引起的错误。可能在 JSF 实现中,或者通过代码中的其他内容。也许你正在使用 <f:ajax>
在命令按钮上,而不是重新呈现输入字段。
关于java - 在 Java Faces 中重置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8021224/