2017-07-28 7 views
0

私は、国、企業、ユーザーなどデータベースのカタログを管理するためのモジュールを構築しようとしています。カタログをコンボボックスから取得すると、システムはプリンシパル列(データベースにNULLでなく、私によって事前に定義されたもの)を持つテーブルを表示することになっています。 3つの目的から、私は2: を達成しました。 を使用してカタログを選択した後、エンティティクラスの@NotNullフィールドを取得します。2.上記からも動的な列を含むテーブルを表示します。 しかし、数字3は私にトラブルを与えています。事は、私は(私は、オブジェクトに格納された@NotNullフィールドに基づいて)動的に列を表示するためのビューでこのfollingコードを使用、である(https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml):(java.lang.Class)クラスをリフレクションで取得したクラスにキャストする方法

<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の名前は、名前、値、IDです。私の列の配列は同じです:名前、値、ID ... 事は、comocomboList<Object>オブジェクトです。フィールドのリフレクションクラスの値を格納するオブジェクトで、EntityClassのインスタンスの代わりにjava.lang.classを返します私はそのクラス(combito)のオブジェクトインスタンスからsetterとgetterを呼び出すことができましたが( )、pojo[column.property] - > comocombo ["id"]、comocombo ["name"]またはcomocombo ["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)); 
      } 
      } 
+0

に対処するために、あなたを忘れないでくださいFaceletsのを願ってコンボボックスの値がentiであるコンボボックスで、選択した値に基づいてデータテーブルの列を動的にロードしたいクラス? – Kaizen

+0

はい@Kaizen、私はすでにこれを達成しています:自分のデータテーブルに列を動的に追加...各エンティティクラスに応じて選択...私のオブジェクトはJavaであるため、これらの列に従ってデータを表示しようとすると問題が発生しました。 lang.Class(反射の効果)とp:dataTableの変数はプロパティに到達しません... –

+0

あなたの質問に答えて受け入れることができます:) – Kaizen

答えて

0

私はこのように動作するように地雷を得たが、それは

private void createDynamicColumns(){ 
      //split column templates 
      String[] columnKeys = columnTemplate.split(" "); 
      //set first list to null 
      //this should be a List with no type specified 
      //eg List objects = new ArrayList<>(); 
      //since you will dynamically populate it based on what the user selects from combobox 
      objects = null; 
      //clear columns 
      columns.clear(); 
      //create a class from combobox selection 
      Class<?> forName = Class.forName("pathToClass." + comboboxSelection); 
      //get declared fields in that class 
      List<Field> asList = Arrays.asList(forName.getDeclaredFields()); 
      //for each columkeys 
      //if method.getName() can be found in columnKeys 
      //add that columnKey to the new columnList; 
      for(int i =0;i< asList.size();i++) 
       for (String columnKey : columnKeys) { 
        String fieldName = asList.get(i).getName(); 
        if (columnKey.equalsIgnoreCase(fieldName)) { 
         columns.add(fieldName); 
        } 
       } 

      //fetch from the database the objects list 
      objects = DAO.findAll(); 
     } 

を助け、私はこのことを理解しなければならない場合は、ページ

<p:dataTable var="object" value="#{backingBean.objects}">      
     <p:columns value="#{BackingBean.columns}" var="column"> 
      <f:facet name="header"> 
       <h:outputText value="#{column}" /> 
      </f:facet> 
      <h:outputText value="#{object[column]}" /> 
     </p:columns> 
</p:dataTable> 

は、例外

+0

*拍手する*ありがとう!私は実際にあなたがそれをしたときのように、このソリューションを実装していませんが、これは: 'value ="#{backingBean.objects} "'必要だっただけです! = D私はとても感謝して幸せです。私はこの行を私の上で変更します: '

+0

このy: 'y = tabla.get(tabla.size() - 1).getConcpetos(); //クエリの型の結果: –

関連する問題