Java 验证器工作不正常

Java 验证器工作不正常,java,validation,jsf,Java,Validation,Jsf,我有一个JSF验证器,用于验证表单中的输入 当我插入简单字符串或重复字符串时,它工作正常。但是当我不输入任何内容时,正确的消息将是此字段不能为空!“:”““+s+””不是有效的名称但我什么也没得到。你能帮我找出代码逻辑中的问题吗 // Validate Datacenter Name public void validateDatacenterName(FacesContext context, UIComponent component, Object value) throws Va

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

当我插入简单字符串或重复字符串时,它工作正常。但是当我不输入任何内容时,正确的消息将是
此字段不能为空!“:”““+s+””不是有效的名称但我什么也没得到。你能帮我找出代码逻辑中的问题吗

// 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));
        }

    }

那是因为你只检查你的s是不是!=空

if(s!=null)
更改为
if(s!=null&&s.lenght()>0)
,然后重试

顺便说一句,您的
字符串s
不能为空,因为您使用

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

如果
值为null,这将导致NullPointerException。

这是因为您只检查s是否为空

if(s!=null)
更改为
if(s!=null&&s.lenght()>0)
,然后重试

顺便说一句,您的
字符串s
不能为空,因为您使用

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

如果
值为空,这将导致NullPointerException。

inputText
标记属性
必需
设置为
true

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

inputText
标签属性
必填项设置为
true

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

什么是“工作不正常”。什么不起作用?您希望您的代码做什么?它在做什么?什么是“工作不正常”。什么不起作用?您希望您的代码做什么?它在干什么?