2

我有一个带有 Java 对象的 CDI bean,我用它来显示来自数据库的配置文件数据:

父 Bean

@Named("DCProfileTabGeneralController")
@ViewScoped
public class DCProfileTabGeneral implements Serializable
{

    public DCObj dc;

    public class DCObj
    {

        private int componentStatsId;                 // COMPONENTSTATSID   NUMBER(38,0)
        ........
        // Default Constructor
        public DCObj(){};

        public DCObj(int componentStatsId....)
        {

            this.componentStatsId = componentStatsId;
            .......

        }

        public int getComponentStatsId()
        {
            return componentStatsId;
        }

        public void setComponentStatsId(int componentStatsId)
        {
            this.componentStatsId = componentStatsId;
        }
        ....
    }

    // Getters ------------------------------------------------------------------------------------
    public DCObj getdcData()
    {
        return dc;
    }

    @PostConstruct
    public void initData() throws SQLException
    {
        initDBData();
    }

    // Generate data Object from Oracle
    public void initDBData() throws SQLException
    {
                dc = new DCObj(result.getInt("COMPONENTSTATSID"),
                .........
    }
}

验证器

@Named("ValidatorDatacenterController")
@ViewScoped
public class ValidatorDatacenter implements Validator, Serializable
{

    public ValidatorDatacenter()
    {
    }

    @Inject
    private DCProfileTabGeneral profileTabGeneral;


    // Validate Datacenter Name
    public void validateDatacenterName(FacesContext context, UIComponent component, Object value) throws ValidatorException, SQLException
    {
        int componentStatsId = -1;

        if (profileTabGeneral != null)
        {
            DCObj dcData = profileTabGeneral.dc;
            if (dcData != null)
            {
                componentStatsId = dcData.getComponentStatsId();
            }
        }

        if (componentStatsId == -1)
        {
            return;
        }

        String l;
        String s;

        if (value != null && !(s = value.toString().trim()).isEmpty())
        {

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

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

            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs;
            int resComponentStatsId = -1;
            try
            {
                conn = ds.getConnection();
                // if componentsstatsid <> edited componentstatsid in jsf -> throw validator exception
                ps = conn.prepareStatement("SELECT componentstatsid from COMPONENTSTATS where NAME = ?");
                ps.setString(1, s);
                rs = ps.executeQuery();

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

                if (resComponentStatsId != -1 && resComponentStatsId != componentStatsId)
                {
                    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,
                  "  This field cannot be empty!", null));
        }

    }

}

我有一个自定义验证器,可以将配置文件页面中的输入值检查到数据库中。我测试了使用@Inject 从父页面获取Java 对象并将Ojject 传递给验证器。事实证明,每次使用 @Inject 时都会得到空的 Java 对象。

我还测试了使用 CDI 获取 Int。它可以工作,但是当我再次测试以再次获取 Java 对象时,我得到了空对象。

你能告诉我从 CDI bean 调用 Java 对象的正确方法是什么吗?为什么我不能从 CDI bean 获取 Java 对象?

4

1 回答 1

4

如果我没记错的话,CDI 注入将无法与验证器一起使用。使用来自 Myfaces CODI 的高级,因为来自 deltaspike 的 JSF 模块尚未准备好。https://cwiki.apache.org/EXTCDI/jsf-usage.html

或者去 deltaspike 并使用 BeanProvider 来获取您的实例。

BeanProvider.getContextualReference(DCProfileTabGeneral .class, false);
于 2013-02-03T21:59:10.233 回答