有一个页面(我在其上进行测试)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
lang="ru">
<h:head>
</h:head>
<h:body>
//всякая всячина
<h:form>
<h:inputText id="id1">
<f:validator validatorId="imageValidator" />
</h:inputText>
<h:message for="id1" rendered="true" />
<p:commandButton value="Сохранить" />
</h:form>
//всякая всячина
</h:body>
</html>
豆为她服务
@ManagedBean(name="Events")
@SessionScoped
@FacesValidator("imageValidator")
public class Events implements Serializable, Validator {
//всякая всячина
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, "ахтунг", null);
throw new ValidatorException(facesMessage);
}
}
现在我想实现,当你点击“保存”按钮时,表单会显示一个关于表单填写错误的消息,然后我将添加其余的逻辑。
validate程序进入函数。执行它没有错误。但不显示该消息。那些。<h:message for="id1" rendered="true" />就像它不工作一样。我不明白出了什么问题,一切似乎都与示例中的相同。
问题是 primeFaces 对常规 jsf 不友好。那些。如果你改变它<p:commandButton value="Сохранить" />
,<h:commandButton value="Сохранить" />
一切正常,但它并不漂亮。我将弄清楚 primeFaces 验证是如何工作的
按下
h:commandButton会导致POST执行 -request 并刷新整个页面,而p:commandButton默认情况下它会执行AJAX-request 并且默认情况下不会更新页面上的任何元素。p:commandButton应该添加一个属性update="id1",以便在请求完成后更新状态h:message。另一种选择是指定
ajax="false". 这将导致行为p:commandButton与h:commandButton.