无法加载带注释的类:java.lang.ClassNotFoundException导致的validators.EmailValidator:validators.EmailValidator

时间:2015-03-19 20:18:59

标签: validation jsf custom-validators

我正在尝试实现自定义验证器,如下所示。这是我的EmailValidator.java类中出现的内容,它位于验证程序包中:

package validators;

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;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

@FacesValidator("validators.EmailValidator")
public class EmailValidator implements Validator {

    private static boolean isValidEmailAddress(String email) {
        boolean result = true;
        try {
            InternetAddress emailAddr = new InternetAddress(email);
            emailAddr.validate();
        } catch (AddressException ex) {
            result = false;
        }
        return result;
    }

    /**
     *
     * @param context
     * @param component
     * @param value
     */
    @Override
    public void validate(FacesContext context,
                        UIComponent component,
                        Object value)  throws ValidatorException {
        try {
            String emailAddress = (String)value;

            if (!isValidEmailAddress(emailAddress)) {
                FacesMessage message = new FacesMessage("Email address format is not valid.");
                throw new ValidatorException(message);
            }

        } catch (ClassCastException e) {
            FacesMessage message = new FacesMessage("Value must be a a String.");
            throw new ValidatorException(message);
        }

    }

}

这是我使用此验证器的JSP页面的一部分:

<b:inputText id="email" placeholder="Email" required="true" requiredMessage="Value is required" value="#{createAccountBean.email}">
      <f:validator validatorId="validators.EmailValidator" />
</b:inputText>
<h:message id="emailMsg" style="color: red" for="email" />

但这是GlassFish Server给我的错误:

Severe:   Unable to load annotated class: validators.EmailValidator
Severe:   java.lang.ClassNotFoundException: validators.EmailValidator
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1783)
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1633)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at com.sun.faces.util.Util.loadClass(Util.java:325)
    at com.sun.faces.config.AnnotationScanner.processClassList(AnnotationScanner.java:309)
    at com.sun.faces.config.DelegateToGlassFishAnnotationScanner.getAnnotatedClasses(DelegateToGlassFishAnnotationScanner.java:85)
    at com.sun.faces.config.DelegatingAnnotationProvider.getAnnotatedClasses(DelegatingAnnotationProvider.java:85)
    at com.sun.faces.config.ConfigManager$AnnotationScanTask.call(ConfigManager.java:932)
    at com.sun.faces.config.ConfigManager$AnnotationScanTask.call(ConfigManager.java:884)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:383)
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:221)
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:5394)
    at com.sun.enterprise.web.WebModule.contextListenerStart(WebModule.java:743)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:5932)
    at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
    at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
    at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:500)
    at com.sun.enterprise.v3.server.ApplicationLoaderService.processApplication(ApplicationLoaderService.java:406)
    at com.sun.enterprise.v3.server.ApplicationLoaderService.postConstruct(ApplicationLoaderService.java:243)
    at org.jvnet.hk2.internal.ClazzCreator.postConstructMe(ClazzCreator.java:329)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:377)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:461)
    at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:227)
    at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:84)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2258)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:105)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
    at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1162)
    at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1147)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我不确定我做错了什么。任何提示都将不胜感激。

1 个答案:

答案 0 :(得分:-1)

尝试将 ClassNotFoundException 更改为例外