我创建了一个新的验证器:
package com.example.jsf.validator;
import com.example.components.LoginFormValue;
import com.example.ejb.SecurityEjb;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.inject.Named;
/**
* Validator for {@code login-form} component.
*
* @author steve
*/
@Named
@ApplicationScoped
public class LoginValidator implements Validator, Serializable
{
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
{
if (value == null)
{
return;
}
LoginFormValue loginFormValue = (LoginFormValue) value;
if (securityEjb.checkCredentials(loginFormValue.getEmailAddress(), loginFormValue.getPassword())) {
return;
}
throw new ValidatorException(new FacesMessage("Incorrect email address/password"));
}
@EJB
private SecurityEjb securityEjb;
}
但是当我尝试使用它时:
<my:login-form emailAddress="#{loginBean.emailAddress}"
rememberMe="#{loginBean.rememberMe}"
actionListener="#{loginBean.submit()}"
recoverPasswordOutcome="recover-password"
registerOutcome="signup">
<f:validator validatorId="#{loginValidator}"/>
</my:login-form>
加载页面时显示此异常:
javax.faces.FacesException: Expression Error: Named Object: com.example.jsf.validator.LoginValidator@31888ef8 not found.
有问题的代码行是:
com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:1593)
为什么 JSF 可以实际解析验证器 bean 类却不能实例化它?我的应用程序中的每个其他验证器都是一个Named
ApplicationScoped
bean,它们都工作得很好。我引用它们都使用<f:validator validatorId="{myValidatorBean}"/>
.
我正在使用 GlassFish 3.1.2。