具有自定义方法的JSF验证器

具有自定义方法的JSF验证器

本文介绍了具有自定义方法的JSF验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Bean的片段:

This is fragment of my Bean:

public class WaiterBean extends Connector
{
    private String PIN;
    private String name = null;

    public void setPIN(String PIN)
    {
        this.PIN = PIN;
    }
    public String getPIN()
    {
        return PIN;
    }
    public String getName()
    {
        return name;
    }
    public String isPINCorrect(String PIN)
    {
        try
        {
            resultSet = statement.executeQuery(
                "SELECT name FROM dbo.waiters WHERE PIN=" + PIN);
            while(resultSet.next())
            {
                name = resultSet.getString("name");
            }
        }
        catch (SQLException se)
        {
            System.out.println(se.getMessage() + "**"
                + se.getSQLState() + "**" + se.getErrorCode());
        }
        if(name == null)
            return "invalid";
        else
            return "valid";
    }
}

这是验证器bean:

public class PINValidator implements Validator
{

    @Override
    public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException
    {
        String PIN = o.toString();
        if(PIN.length() < 4)
        {
            FacesMessage msg = new FacesMessage("PIN too short");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }
}

这就是我的用法:

<h:form>
    <h:panelGrid columns="2">
        ENTER PIN
        <h:inputText id="PIN" maxlength="4" size="4" value="#{waiterBean.PIN}">
            <f:validator validatorId="com.jsf.PINValidator" />
        </h:inputText>
    </h:panelGrid>
    <h:message for="PIN"/>
    <br />
    <h:commandButton value="SEND" action="#{waiterBean.isPINCorrect(waiterBean.PIN)}" />
    <br />
</h:form>

一切正常,但是我认为在验证器类中包括isPINCorrect方法是一种很好的做法(我错了吗?).我可以在验证器中实现该方法,但是然后出现一个问题,如何为WaiterBean设置setName,它对于应用程序是必需的.

Everything works fine, but I think it's good practice to include the isPINCorrect method in the validator class (am I wrong?). I can implement the method in the validator, but then I have a problem how to setName for the WaiterBean and it's needed for the application.

我应该如何解决该问题?还是另一个问题,我什至应该尝试解决吗?

How should I resolve the problem? Or another question, should I even try to resolve it?

推荐答案

您可以从验证器访问会话范围内的受管Bean,如下所示:

You can access a session scoped managed bean from a validator like this:

facesContext.getExternalContext().getSessionMap().get("waiterBean");

但是我认为这不是您的最佳做法,因为验证器不应修改数据,而应仅检查输入的有效性.这样做的主要原因是,在验证阶段,模型尚未更新,在验证器中修改bean可能会产生一些副作用.

But I don't think this is the best practice in your case because a validator should not modify data, should only check for the validity of the input. The main reason for this at the validation phase the model is not updated yet, and modifying your bean in the validator can cause some side-effects.

看看 JSF生命周期

这篇关于具有自定义方法的JSF验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:52