2

1 月に投稿した質問に多少関係する別の質問があります。rich:extendedDataTable コンポーネントであるリストがあり、ユーザーが別のテキスト ボックスに検索条件を入力すると、オンザフライで更新されます (つまり、ユーザーは最初の 4 文字を入力し、入力を続けると結果が表示されます)。リストの変更)。結局、RichFaces 3 を使用すると問題なく動作しますが、RichFaces 4 にアップグレードすると、あらゆる種類のコンパイルの問題が発生します。次のクラスにはアクセスできなくなり、これらに代わる適切なクラスはないようです。

org.richfaces.model.DataProvider
org.richfaces.model.ExtendedTableDataModel
org.richfaces.model.selection.Selection
org.richfaces.model.selection.SimpleSelection

これが以前のものです:

これは、検索ロジックをトリガーする入力テキストです。

<h:inputText id="firmname" value="#{ExtendedTableBean.searchValue}">
   <a4j:support ajaxSingle="true" eventsQueue="firmListUpdate"  
                reRender="resultsTable" 
                actionListener="#{ExtendedTableBean.searchForResults}" event="onkeyup" />
</h:inputText>

アクション リスナーは、リストを更新する必要があります。inputText のすぐ下にある extendedDataTable は次のとおりです。

<rich:extendedDataTable tableState="#{ExtendedTableBean.tableState}" var="item"
                       id="resultsTable" value="#{ExtendedTableBean.dataModel}">

        ... <%-- I'm listing columns here --%>

</rich:extendedDataTable>

データ モデルの処理を使用するバックエンド コードは次のとおりです。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.beans;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;

public class ExtendedTableBean {      
    private String sortMode="single";
    private ExtendedTableDataModel<ResultObject> dataModel;
    //ResultObject is a simple pojo and getResultsPerValue is a method that 
    //read the data from the properties file, assigns it to this pojo, and
    //adds a pojo to the list 

    private Object tableState;
    private List<ResultObject> results = new CopyOnWriteArrayList<ResultObject>();
    private List<ResultObject> selectedResults = 
                                      new CopyOnWriteArrayList<ResultObject>();

    private String searchValue;

    /**
     * This is the action listener that the user triggers, by typing the search value
     */
    public void searchForResults(ActionEvent e) {
       synchronized(results) {
           results.clear();
       }        

       //I don't think it's necessary to clear results list all the time, but here
       //I also make sure that we start searching if the value is at least 4 
       //characters long
       if (this.searchValue.length() > 3) {
           results.clear();
           updateTableList();
       } else {
           results.clear();
       }

       dataModel = null; // to force the dataModel to be updated.
    }

    public List<ResultObject> getResultsPerValue(String searchValue) {
        List<ResultObject> resultsList = new CopyOnWriteArrayList<ResultObject>();

        //Logic for reading data from the properties file, populating ResultObject
        //and adding it to the list

        return resultsList;
    }

    /**
     * This method updates a firm list, based on a search value
     */
    public void updateTableList() {
        try {              
            List<ResultObject> searchedResults = getResultsPerValue(searchValue);

            //Once the results have been retrieved from the properties, empty 
            //current firm list and replace it with what was found.

            synchronized(firms) {
                firms.clear();
                firms.addAll(searchedFirms);
            }
        } catch(Throwable xcpt) {
            //Exception handling
        }
    }

    /**
     * This is a recursive method, that's used to constantly keep updating the 
     * table list.
     */
    public synchronized ExtendedTableDataModel<ResultObject> getDataModel() {
        try {
            if (dataModel == null) {
                dataModel = new ExtendedTableDataModel<ResultObject>(
                               new DataProvider<ResultObject>() {
                                   public ResultObject getItemByKey(Object key) {
                                       try {
                                           for(ResultObject c : results) {
                                               if (key.equals(getKey(c))){
                                                   return c;
                                               }
                                           }
                                       } catch (Exception ex) {
                                           //Exception handling
                                       }
                                       return null;
                                   }

                                   public List<ResultObject> getItemsByRange(
                                                 int firstRow, int endRow) {
                                       return Collections.unmodifiableList(results.subList(firstRow, endRow));
                                   }

                                   public Object getKey(ResultObject item) {
                                       return item.getResultName();
                                   }

                                   public int getRowCount() {
                                       return results.size();
                                   }
                              });
            }
        } catch (Exception ex) {
            //Exception handling    
        }

        return dataModel;
    }

    //Getters and setters 

}

クラス ExtendedTableDataModel と DataProvider が利用できなくなったので、代わりに何を使用すればよいでしょうか? RichFaces フォーラムは、そこには実際には何もなく、開発者はほとんど独力であると主張しています (つまり、独自の実装を行う必要があります)。他にアイデアや提案はありますか?

長い質問で申し訳ありませんが、ご協力いただきありがとうございます。

4

1 に答える 1

1

データモデルを変換して抽象を拡張することもできますがorg.ajax4jsf.model.ExtendedDataModel、これは実際には で使用するためのより堅牢でパフォーマンスの高いデータモデルです<rich:extendedDataTable/>。以下は、既存のモデルから新しいモデルへの大まかな翻訳です (翻訳を示すために、リストExtendedDataModel<ResultObject>の代わりに既存のモデルを基になるデータ ソースとして使用することにしました)。results

   public class MyDataModel<ResultObject> extends ExtendedDataModel<ResultObject>{

    String currentKey; //current row in the model
    Map<String, ResultObject> cachedResults = new HashMap<String, ResultObject>(); // a local cache of search/pagination results
    List<String> cachedRowKeys; // a local cache of key values for cached items
    int rowCount;
    ExtendedTableDataModel<ResultObject> dataModel; // the underlying data source. can be anything


    public void setRowKey(Object item){
     this.currentKey = (ResultObject)item.getResultName();   
    }

    public void walk(FacesContext context, DataVisitor visitor, Range range, Object argument) throws IOException {
    int firstRow = ((SequenceRange)range).getFirstRow();
    int numberOfRows = ((SequenceRange)range).getRows();
    cachedRowkeys = new ArrayList<String>();
    for (ResultObject result : dataModel.getItemsByRange(firstRow,numberOfRows)) {
       cachedRowKeys.add(result.getResultName());
       cachedResults.put(result.getResultName(), result); //populate cache. This is strongly advised as you'll see later.
       visitor.process(context, result.getResultName(), argument);
      }
    }

   }


 public Object getRowData() {
   if (currentKey==null) {
       return null;
   } else {
       ResultObject selectedRowObject = cachedResults.get(currentKey); // return result from internal cache without making the trip to the database or other underlying datasource
       if (selectedRowObject==null) {  //if the desired row is not within the range of the cache

           selectedRowObject = dataModel.getItemByKey(currentKey);
           cachedResults.put(currentKey, selectedRowObject);
           return selectedRowObject;
       } else {
           return selectedRowObject;
       }
   }

 public int getRowCount(){
 if(rowCount == 0){
    rowCount = dataModel.getRowCount(); //cache row count
    return  rowCount;
  }
  return rowCount

 }

これらは、そのクラスで最も重要な 3 つのメソッドです。他にもたくさんの方法がありますが、基本的にはレガシーバージョンから引き継いでおり、心配する必要はありません。JSF の状態をクライアントに保存する場合はorg.ajax4jsf.model.SerializableDataModel、シリアル化の目的で に関心があるかもしれません。ここでその例を参照してください。古いブログですが、ロジックはまだ適用可能です。

これとは関係ありませんが、現在の の実装はgetRowData製品グレードのアプリではうまく機能しません。結果を返すためにすべての要素を反復する必要がありますか? より良い検索アルゴリズムを試してください。

于 2012-11-14T04:45:36.033 に答える