我想使用自定义JSF验证程序显示错误消息。
验证后,我没有留言!
EDITED
我添加了更多xHTML代码,我有一个包含许多字段和a4j:commandButton
的弹出窗口。
xHTML
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:sec="http://www.springframework.org/security/facelets/tags"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<body>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<ui:composition>
<h:form>
<ui:debug hotkey="x" />
<h:panelGrid width="100%"
rendered="#{not empty declarationReglementaireModel.detailCurrentDecReg.decReg.listLigneDipRecsDTO}">
<rich:extendedDataTable id="listDipRec"
iterationStatusVar="itDipRec" rows="50"
value="#{declarationReglementaireModel.ligneDipRec}"
var="ligneDipRec" frozenColumns="1"
style="height:300px; width:900px;" selectionMode="none">
<rich:column width="35px">
<h:panelGrid columns="1" cellpadding="2">
<a4j:commandLink render="editGridDipRec" execute="@this"
oncomplete="#{rich:component('modifDipRec')}.show()">
<span class="icone icone-edit icone-align-center" />
<a4j:param value="#{itDipRec.index}"
assignTo="#{declarationReglementaireModel.currentLigneDipRecIndex}" />
<f:setPropertyActionListener
target="#{declarationReglementaireModel.currentLigneDipRec}"
value="#{ligneDipRec}" />
</a4j:commandLink>
</h:panelGrid>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="F106" />
</f:facet>
<h:outputText value="#{ligneDipRec.lbValeurRubriqueF106}">
</h:outputText>
</rich:column>
</rich:extendedDataTable>
</h:panelGrid>
<rich:popupPanel header="Données modifiables" id="modifDipRec"
domElementAttachment="form" autosized="true" zindex="500"
left="auto" top="auto">
<h:panelGrid columns="1" id="editGridDipRec">
<h:panelGrid columns="6" cellspacing="10"
styleClass="criteresSaisie" rowClasses="critereLigne"
columnClasses="titreCourtColonne,,titreCourtColonne,,titreCourtColonne,">
<h:outputLabel for="DipRecDtLigne" value="Date ligne fichier Dip " />
<h:outputText id="DipRecDtLigne"
value="#{declarationReglementaireModel.currentLigneDipRec.dtLigneDipRec}">
<f:convertDateTime pattern="dd/MM/yyyy" timeZone="Europe/Paris" />
</h:outputText>
<h:outputLabel for="DipRecLbF106" value="F106 " />
<h:inputText id="DipRecLbF106"
value="#{declarationReglementaireModel.currentLigneDipRec.lbValeurRubriqueF106}">
<f:validator validatorId="checkvalidedouble" />
</h:inputText>
<h:messages id="DipRecLbF106_message" for="DipRecLbF106" />
</h:panelGrid>
<h:panelGroup>
<div align="right">
<h:panelGrid columns="8">
<a4j:commandButton value="Enregistrer"
action="#{rechercheDecRgltCtrl.enregistrerLigneDipRec}"
render="listDipRec DipRecLbF106_message" execute="modifDipRec"
oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('modifDipRec')}.hide();}" />
<a4j:commandButton value="Annuler"
onclick="#{rich:component('modifDipRec')}.hide(); return false;" />
</h:panelGrid>
</div>
</h:panelGroup>
</h:panelGrid>
</rich:popupPanel>
</h:form>
</ui:composition>
</body>
</html>
验证器
public void validate(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException{
String enteredValue = (String)object;
if(enteredValue.contains(",")){
enteredValue = enteredValue.replace(",", ".") ;
}
boolean isANumber = false;
try {
double d = Double.parseDouble(enteredValue);
isANumber = true;
}catch(NumberFormatException nfe) {
isANumber = false;
}
if (isANumber == false) {
FacesMessage msgErreur = new FacesMessage("La valeur n'est pas un nombre valide.") ;
msgErreur.setSeverity(FacesMessage.SEVERITY_ERROR) ;
facesContext.addMessage(null, msgErreur) ;
throw new ValidatorException(msgErreur);
}
}
面-配置
<validator>
<validator-id>checkvalidedouble</validator-id>
<validator-class>xx.xxx.xxxxxx.model.DoubleValidator</validator-class>
</validator>
答案 0 :(得分:0)
尝试将另一个字符串参数传递给FacesMessage构造函数,第二个字符串是将在客户端显示的字符串。
FacesMessage msgErreur = new FacesMessage("La valeur n'est pas un nombre valide.", "validation error msg") ;