我正在使用 JSF 2.1.21,当我在输入文本框上按 Enter 时,我的错误消息会显示并立即清除。我在输入上有一个javascript,它触发了一个命令按钮。
<h:form>
<div><ice:messages /></div>
<ice:inputText onkeyup="submitOnEnter(event);" value="#{bean.name}" maxlength="6" size="6">
<f:validator validatorId="stringValidator" />
</ice:inputText>
<ice:panelGroup>
<ice:commandButton id="button" value="Go" actionListener="#{myController.getReport}">
</ice:commandButton>
</ice:panelGroup>
</h:form>
字符串验证器:
public void validate(FacesContext context, UIComponent component, Object value) throws
ValidatorException {
if (!(((String) value).trim()).matches(REGEX_CONSTANT)) {
((UIInput) component).setValid(false);
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
myMessage, null));
} else {
((UIInput) component).setValid(true);
}
更新:
function submitOnEnter(event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if(keyCode == 13)
{
document.getElementById("myForm:button").click();
return false;
}
}