【问题标题】:Validator not working properly验证器无法正常工作
【发布时间】:2013-02-09 11:33:05
【问题描述】:

我有一个 JSF 验证器,用于验证来自表单的输入。

当我插入简单字符串或重复字符串时,它工作正常。但是当我没有输入任何内容时,正确的消息将是This field cannot be empty!" : " '" + s + "' is not a valid name!,但我什么也没有得到。你能帮我找出代码逻辑的问题吗?

// Validate Datacenter Name
    public void validateDatacenterName(FacesContext context, UIComponent component, Object value) throws ValidatorException, SQLException
    {

        String l;
        String s = value.toString().trim();

        if (s != null && s.length() > 18)
        {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "  Value is too long! (18 digits max)", null));
        }

        try
        {
//            l = Long.parseLong(s);
//            if (l > Integer.MAX_VALUE)
//            {
//                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
//                        "  '" + l + "' is too large!", null));
//            }
        }
        catch (NumberFormatException nfe)
        {
            l = null;
        }


        if (s != null)
        {

            if (ds == null)
            {
                throw new SQLException("Can't get data source");
            }

            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs;
            int cnt = 0;
            try
            {
                conn = ds.getConnection();
                ps = conn.prepareStatement("SELECT count(1) from COMPONENTSTATS where NAME = ?");
                ps.setString(1, s);
                rs = ps.executeQuery();

                while (rs.next())
                {
                    cnt = rs.getInt(1);
                }

                if (cnt > 0)
                {
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "  '" + s + "' is already in use!", null));
                }

            }
            catch (SQLException x)
            {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "  SQL error!", null));
            }
            finally
            {
                if (ps != null)
                {
                    ps.close();
                }
                if (conn != null)
                {
                    conn.close();
                }
            }
        }
        else
        {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    s.isEmpty() ? "  This field cannot be empty!" : "  '" + s + "' is not a valid name!", null));
        }

    }

【问题讨论】:

  • “无法正常工作”是什么意思。什么不工作?你想让你的代码做什么?它在做什么

标签: java validation jsf


【解决方案1】:

那是因为您只检查 s 是否为 != null。

if (s != null) 更改为 if (s != null && s.lenght() > 0) 并重试。

顺便说一句,你的String s 不能为空,因为你用它初始化它

String s = value.toString().trim();

如果您的 value 为空,这将导致 NullPointerException。

【讨论】:

    【解决方案2】:

    inputText标签属性required设置为true

    <h:inputText value="#{backingBean.input}" required="true" 
            requiredMessage="Input is empty!">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多