我在 jsf 中有一个用于下拉菜单的自定义验证器。但是这个自定义验证器永远不会被调用。
下面是具有自定义验证器 categoryValidator 的代码
<?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:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<h:title>Add Category</h:title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel id="catNameLabel" value="Category name" for="j_catName"/>
<h:inputText id="j_catName" value="#{categoryActionBean.category.name}" required="true"
label="Cateory name">
<f:validateRequired />
</h:inputText>
<h:message for="j_catName" style="color:red;"/>
<h:outputLabel id="catDescLabel" value="category Description" for="j_catDesc"/>
<h:inputText id="j_catDesc" value="#{categoryActionBean.category.description}" required="true"
label="Category Description">
<f:validateRequired />
</h:inputText>
<h:message for="j_catDesc" style="color:red;"/>
<h:outputLabel id="selParentCatLabel" value="Parent Category" for="j_selParentCat"/>
<h:selectOneMenu id="j_selParentCat" value="#{categoryActionBean.selectedParentCategory}"
label="Selected category">
<f:validator validatorId="categoryValidator"/>
<f:selectItem itemValue="" itemLabel="">
<f:param name="isParent" value=""/>
</f:selectItem>
<c:forEach items="#{categoryActionBean.categories}" var="catMap">
<f:selectItem itemValue="#{catMap.key}" itemLabel="#{catMap.value.name}">
<f:param name="isParent" value="false"/>
</f:selectItem>
</c:forEach>
</h:selectOneMenu>
<h:message for="j_selParentCat" style="color:red;"/>
</h:panelGrid>
<h:commandButton value="Save category" action="#{categoryActionBean.saveCategory}" type="submit"/>
</h:form>
</h:body>
</html>
下面是我的自定义验证器。
package com.auction.jsf.validator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator("categoryValidator")
public class CategoryValidator implements Validator{
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
System.out.println("Inside category validator");
// My validation logic
---
---
---
if(validationFalied) {
throw new ValidatorException(facesMessage);
}
}
}
不调用选择菜单的 categoryValidator。任何帮助表示赞赏。