【发布时间】:2013-11-28 20:22:30
【问题描述】:
在我的应用程序中,我有以下 Validator 来验证验证码输入:
@Named(value = "simpleCaptchaValidator")
@RequestScoped
public class SimpleCaptchaValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
Captcha secretCaptcha = (Captcha) session.getAttribute(Captcha.NAME);
// Clear the input field
EditableValueHolder input = (EditableValueHolder) component;
input.resetValue();
// Display an error msg if the entered words are wrong
if (!secretCaptcha.isCorrect(value.toString())) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed!", "You have entered wrong words.");
throw new ValidatorException(msg);
}
}
}
当用户输入错误的验证码时,上述Validator 非常适用于非 Ajax 请求。但是,如果用户输入了正确的验证码,但在其他组件上验证失败,则验证码的输入字段不会被清除。
如果您能告诉我如何解决上述问题,我将不胜感激。
【问题讨论】:
-
请显示一些相关的视图代码。
-
好吧,我想如果验证码无效,您可能想从会话中删除该值? session.setAttribute(Captcha.NAME) = "";
标签: jsf-2 input reset validation