0

我正在使用 primefaces 3.5,带有 glassfish 服务器 3.1.2。我有一个依赖用户选择答案的琐事问题游戏。我有两个表,其中一个是根据它是多选问题还是它的多选问题生成的。虽然我的多项选择数据表工作得很好,但另一个没有。我已经按照展示案例中的示例进行操作,当我选择表中的 2 个并点击它所在的向导上的下一步按钮时,它会取消选择我选择的内容并让我保持在同一页面上。我让它在任何异常上都保持在同一页面上,并且异常是一个空指针,因为“选定的答案”是空的。这是我的桌子。

<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;
}

setter 永远不会被调用,但使用的数据模型非常适合单选。如果需要解决我的问题,请告诉我。如果可能,请提供帮助。

公共类 QuestionAnswersDataModel 扩展 ListDataModel 实现 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();
}
4

0 回答 0