我正在使用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>
stringValidator:
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;
}
}