自定义验证器无法使用sjf中的下拉菜单

时间:2013-03-04 17:36:39

标签: jsf-2

我在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);

         }
        }

}

不会调用select菜单的categoryValidator。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

您需要在faces-config.xml

中注册自定义验证程序
<validator>
    <validator-id>customValidator</validator-id>
    <validator-class>path to this categoryValidator class</validator-class>
</validator>

或者您可以直接在Jsf 2.0 +版本中进行注释

 @FacesValidator("path to this categoryValidator class")
    public class CategoryValidator {

}

但不要两者都做。即使你这样做,faces-config.xml优先。

答案 1 :(得分:0)

您可以对selectedParentCategory使用Bean验证(JSR 303), 请参阅Jeetutorial6 Pag 206上的使用BeanValidation。