【问题标题】:JSF Custom Validator: h:message is not being renderedJSF 自定义验证器:h:message 未呈现
【发布时间】: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


【解决方案1】:

您什么也看不到,因为您已经构建了FacesMessage,其中包含一个摘要一个详细信息。当详细信息不是null 时,将改为显示。由于您已将其设置为空字符串,因此您“看到”了一条空消息。您基本上需要将详细信息设置为null 才能显示摘要。

但这并不是您应该在验证错误上设置消息的方式。您应该只需抛出 ValidatorException,JSF 将根据组件的客户端 ID 将 ValidatorException 构造的消息添加到上下文本身。

所以,你需要更换

FacesMessage msg = new FacesMessage(
        FacesMessage.SEVERITY_ERROR,
        "The old password was not entered correctly.",
        ""
);
context.addMessage(comp.getClientId(context), msg);
throw new ValidatorException(msg);

通过

String msg = "The old password was not entered correctly.";
throw new ValidatorException(new FacesMessage(msg));

它会按您的预期工作。

【讨论】:

  • 你帮了我很多。非常感谢!
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 2012-01-21
  • 1970-01-01
  • 2015-12-01
  • 2013-07-02
相关资源
最近更新 更多