Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 自定义验证器不使用sjf中的下拉菜单_Jsf 2 - Fatal编程技术网

Jsf 2 自定义验证器不使用sjf中的下拉菜单

Jsf 2 自定义验证器不使用sjf中的下拉菜单,jsf-2,Jsf 2,我在jsf中有一个用于下拉菜单的自定义验证器。但是这个自定义验证器永远不会被调用 下面是具有自定义验证器类别validator的代码 <?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

我在jsf中有一个用于下拉菜单的自定义验证器。但是这个自定义验证器永远不会被调用

下面是具有自定义验证器类别validator的代码

<?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>      

未调用select菜单的categoryValidator。非常感谢您的帮助。

您需要在
faces config.xml

<validator>
    <validator-id>customValidator</validator-id>
    <validator-class>path to this categoryValidator class</validator-class>
</validator>

但不要同时做这两件事。即使你这样做,faces config.xml也会优先考虑。

你可以对selectedParentCategory使用Bean验证(JSR 303),
请参阅第206页的JEETORIAL6上的使用BeanValidation。

您是否在
faces config.xml
中注册了验证程序?是的,我已使用注释FacesValidator对我的验证程序进行了注释。如果验证器用于输入文本,则会调用它,但不会为选择菜单调用它。您可以将验证器类添加到问题中吗?我已将我的验证器类添加到问题中。
<validator>
    <validator-id>customValidator</validator-id>
    <validator-class>path to this categoryValidator class</validator-class>
</validator>
 @FacesValidator("path to this categoryValidator class")
    public class CategoryValidator {

}