私は、グラスフィッシュサーバー3.1.2でprimefaces 3.5を使用しています。ユーザーが回答を選択することに依存するトリビア質問ゲームがあります。複数選択の質問であるか、複数選択の質問であるかに基づいて生成される 2 つのテーブルがあります。私の多肢選択式データ テーブルは美しく機能しますが、もう一方は機能しません。ショー ケースの例に従いました。テーブルの 2 つを選択し、それが含まれているウィザードの [次へ] ボタンをクリックすると、選択したものが選択解除され、同じページにとどまります。例外が発生しても同じページにとどまるようにしましたが、「選択された回答」がnullであるという事実により、例外はnullポインターでした。これが私のテーブルです。
<p:dataTable
id="multiQuestionTable"
value="#{triviaQuestionsBean.dataModel}"
var="answer"
selection="#{triviaQuestionsBean.selectAnswers}">
<p:column selectionMode="multiple" />
<p:column>
#{answer.answer.testAnswer}
</p:column>
</p:dataTable>
設定とゲッター:
private QuestionAnswers[] selectAnswers;
public QuestionAnswers[] getSelectAnswers() {
return selectAnswers;
}
public void setSelectAnswers(QuestionAnswers[] selectAnswers) {
this.selectAnswers = selectAnswers;
}
セッターが呼び出されることはありませんが、使用されるデータ モデルは単一の選択に対して非常にうまく機能します。私の問題を理解するためにそれが必要な場合は、お知らせください。可能であればご協力ください。
public class QuestionAnswersDataModel extends ListDataModel implementation SelectableDataModel {
/**
* This is the question answers data model used to allow for the sorting,
* and selection of items in a JSF dataTable. This is the basic no-arg
* constructor --Important-- This judges the data from the id, so if the ID
* has not been assigned, there will be unpredictable results.
*
*/
public QuestionAnswersDataModel() {
}
/**
* This is the question answers data model used to allow for the sorting,
* and selection of items in a JSF dataTable. This is the constructor where
* the list of elements are instantiated. --Important-- This judges the data
* from the id, so if the ID has not been assigned, there will be
* unpredictable results.
*
* @param data The list of QuestionAnswers to display in the table.
*/
public QuestionAnswersDataModel(List<QuestionAnswers> data) {
super(data);
}
/**
* This takes a "row key" and looks through the wrapped data to find the
* specific QuestionAnswers entity that matches the passed in row key
*
* @param rowKey The key to search with
* @return The QuestionAnswers entity that matches the criteria or null if
* nothing matches
*/
@Override
public QuestionAnswers getRowData(String rowKey) {
/**
* Get the wrapped data (If there was a lot of data you would use a
* query not just a list)
*/
List<QuestionAnswers> answers =
(List<QuestionAnswers>) getWrappedData();
//for each answer
for (QuestionAnswers answer : answers) {
//if the answer's unique identifier matches the row key:
if (answer.getQuestionAnswersId().toString().equals(rowKey)) {
//return it
return answer;
}
}
//if nothing matches return null
return null;
}
/**
* This takes a QuestionAnswers entity object and returns a key for the
* identification of this entity. As this one runs off of the ID of the
* answer, if nothing is assigned to the value, a null key will be returned.
*
* @param answer The answer to generate the key of
* @return The identifier for this object or null if the ID is null
*/
@Override
public Object getRowKey(QuestionAnswers answer) {
//if the answer is null, return null
if (answer == null) {
return null;
}
//else get the answer id
Long id = answer.getQuestionAnswersId();
//if it's null return null
if (id == null) {
return id;
}
//else return the String representation of the id
return id.toString();
}