【发布时间】:2011-09-04 08:12:52
【问题描述】:
我有以下 FacesValidator:
@RequestScoped
@FacesValidator("passwordValidator")
public class PasswordValidator implements Validator {
@PersistenceUnit(unitName = "TradeCenterPU")
private EntityManagerFactory emf;
@Override
public void validate(final FacesContext context, final UIComponent comp, final Object values) throws ValidatorException {
String password = (String)values;
System.out.println("passwordValidator():" + password);
EntityManager em = emf.createEntityManager();
Query q = em.createNamedQuery("user.findByUsername");
q.setParameter("username", context.getExternalContext().getRemoteUser());
User user = (User)q.getSingleResult();
String pwhash = DigestUtils.md5Hex(password + user.getSalt());
System.out.println("User: " + user.getUsername() + ", PwHash: " + pwhash + ", Password: " + user.getPassword());
if (!pwhash.equals(user.getPassword())) {
System.out.println(comp.getClientId(context) + ": Old password is wrong!");
FacesMessage msg = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"The old password was not entered correctly.",
""
);
context.addMessage(comp.getClientId(context), msg);
throw new ValidatorException(msg);
}
}
}
Wich 的使用方式如下:
<h:form id="profileform" action="#{userController.updatePassword}">
<h:messages errorClass="error_message" globalOnly="true"/>
...
<h:outputLabel for="password" value="Old password:" />
<h:inputSecret id="password" name="password" label="Old password">
<f:validateLength minimum="8" maximum="15" />
<f:validator validatorId="passwordValidator"/>
</h:inputSecret>
<h:message for="password" errorClass="error_message"/>
...
</h:form>
现在的问题是,验证器生成的消息永远不会显示。我知道它是生成的,因为在 Glassfish-Log 中我可以看到
profileform:password: Old password is wrong!
我看不到任何错误,尤其是因为f:validateLength 的消息会在密码过长或过短时显示。
如果我这样做了
context.addMessage(null, msg);
而不是
context.addMessage(comp.getClientId(context), msg);
消息显示在h:messages 组件中。
有人有想法吗?提前致谢
【问题讨论】:
-
只是想知道,您使用的是 Seam 3 吗?由于通常 PhaseListener 不是注入目标,因此注入实体管理器工厂将不起作用。
标签: java validation jsf jsf-2