我正在尝试构建一个模块来管理我的数据库目录,例如:国家、企业、用户等。用户应该从组合框中选择目录,系统应该显示一个包含主要列的表(在数据库和一些我预定义的)。从 3 个目标中,我只实现了 2:1.- 在@NotNull
使用反射选择目录后从实体类中获取字段 2.-显示带有动态列的表,也从上面检索它们。但是3号给我带来了麻烦。问题是,我在视图中使用以下代码来动态显示列(基于@NotNull
我存储在对象中的字段),(https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml ):
<p:dataTable id="conceptos" var="pojo" value="#{catalogoMB.comocombo}>
<p:columns value="#{catalogoMB.columns}" var="column"
columnIndexVar="colIndex" sortBy="#{pojo[column.property]}" filterBy="#
{pojo[column.property]}">
<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>
<h:outputText value="#{pojo[column.property]}" />
</p:columns>
</p:dataTable>
因此,例如,以正常方式,没有反射,上面的代码将像这样工作:
comocombo将具有以下属性:name、value、id;并且我的列数组将是相同的:名称、值、id ......问题是,comocombo
是一个 List<Object>
我存储字段反射类值的对象,它返回 java.lang.class 而不是 EntityClass 的实例,尽管我管理从该类的 Object 实例(combito)调用 setter 和 getter -supposedly - 所以当我尝试显示pojo[column.property]
-> comcomombo["id"], comocombo["name"] 或 comcomombo["value"] 它向我发送一个例外说java.lang.class
没有任何属性....我怎样才能找到它们?我读过关于Map<String, String>
和的.cast()
但我不确定这可能是这样的。
public void populateT(){
comocombo=new ArrayList<>();
Object tt ;
y = tabla.get(tabla.size()-1).getConcpetos(); //result of query type:
FindAll from the entity Class
try{
Class combito= Class.forName("sipe.services."+ catName); //the "path" of the
Entity Classes
for (Integer j=0; j<y.size()-1; j++){
tt=y.get(j);
for (Integer i=0; i< tabla.size()-1; i++){
tucampo=minustomayus(y.get(j).getClass().getDeclaredField(tabla.get(i).getNombre_c()).getName()); //tabla.get(i).getNombre_c()-> here I've stored the @NotNull properties' names (countryid, countryname...) whic are the same in columns = new ArrayList<ColumnModel>(); (catalogoMB.columns in the view)
Class cls= Class.forName("sipe.services."+ catName);
Method method = cls.getDeclaredMethod("get"+tucampo); // for example "countryid" -> getCountryid
Class<?> type = null;
for (Method methods : combito.getDeclaredMethods())
{ //Here I'm trying to invoke setter of the Entity Class in order to store its values..
//equivalent to: if o is an instance of Entity Class Country: Country o = new Country(); o.setCountryid(2);
if (methods.getName().contains("set"+tucampo)){
type=method.getReturnType();
methods.invoke(combito.newInstance(),method.invoke(tt));
}
}