gpt4 book ai didi

java - 如何从 CDI bean 调用 Java 对象

转载 作者:行者123 更新时间:2023-11-30 07:19:25 25 4
gpt4 key购买 nike

我有一个带有 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));
}

}

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

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

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

最佳答案

如果我没记错的话,CDI 注入(inject)将不适用于 validator 。使用来自 Myfaces CODI 的高级,因为来自 deltaspike 的 JSF 模块尚未准备好。 https://cwiki.apache.org/EXTCDI/jsf-usage.html

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

BeanProvider.getContextualReference(DCProfileTabGeneral .class, false);

关于java - 如何从 CDI bean 调用 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com