简体   繁体   English

JSF 自定义验证器:h:消息未呈现

[英]JSF Custom Validator: h:message is not being rendered

I have the following FacesValidator:我有以下 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 gets used in the following way: 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>

The problem now is, that the message which the Validator generates is never displayed.现在的问题是,验证器生成的消息永远不会显示。 I know it gets generated, because in the Glassfish-Log I can see我知道它会生成,因为在 Glassfish-Log 我可以看到

profileform:password: Old password is wrong!

I can't see any error in this especially since the message for the f:validateLength gets displayed if the password is to long or to short.我看不到任何错误,特别是因为如果密码太长或太短,会显示f:validateLength的消息。 If I do如果我做

context.addMessage(null, msg);

instead of代替

context.addMessage(comp.getClientId(context), msg);

the Message is shown in the h:messages component.消息显示在h:messages组件中。 Has anyone an idea?有人有想法吗? Thanks in advance提前致谢

You see nothing, because you've constructed the FacesMessage with a summary and a detail.您什么也看不到,因为您已经构造了带有摘要详细信息的FacesMessage When the detail is not null , then it will be displayed instead.当详细信息不是null时,它将改为显示。 Since you've set it with an empty string, you "see" an empty message.由于您已将其设置为空字符串,因此您“看到”了一条空消息。 You basically need to set detail to null to get the summary to display.您基本上需要将详细信息设置为null才能显示摘要。

But this isn't exactly the way you're supposed to set a message on a validation error.但这并不完全是您应该在验证错误上设置消息的方式。 You should just throw the ValidatorException and JSF will add the in the ValidatorException constructed message to the context itself based on the component's client ID.您应该抛出ValidatorException并且 JSF 将根据组件的客户端 ID 将ValidatorException构造的消息添加到上下文本身。

So, you need to replace所以,你需要更换

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

by经过

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

and it will work as you expect.它会按您的预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM