2012-08-30 6 views
4

Primefaces Datatable에 특히 Selection 개체에 대한 질문이 있습니다.Primefaces Datatable 선택 개체

다음 코드에서는 선택 항목이있는 Datatable에 바인딩 된 변수 "Selected Question"에 대해 항상 Null을 갖습니다.

다음과 같이 JSF :

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 

    <ui:composition template="mainTemplate.xhtml"> 
     <ui:define name="contentTitle">Your Questions</ui:define> 
     <ui:define name="content"> 
      <h:form id="formAllQuestion"> 
       <p:growl id="allQuestionGrowl" showDetail="true"/> 

       <p:dataTable id="allQuestionsTable" var="question" value="#{allQuestionBean.allQuestionDataHelper}" paginator="true" rows="10" 
          selection="#{allQuestionBean.selectedQuestion}" selectionMode="single"> 

        <p:ajax event="rowSelect" listener="#{allQuestionBean.onRowSelect}" update=":formAllQuestion:AnswerToQuestionDialogTable :formAllQuestion:allQuestionGrowl" 
          oncomplete="questDialog.show()"/> 
        <p:ajax event="rowUnselect" listener="#{allQuestionBean.onRowUnselect}" update=":formAllQuestion:allQuestionGrowl"/> 


        <f:facet name="header">Select a Row to display your Question Details</f:facet> 

        <p:column headerText="QuestionID"> 
         #{question.questionId} 
        </p:column> 
        <p:column headerText="Question Name"> 
         #{question.questionName} 
        </p:column> 
        <p:column headerText="Question Description"> 
         #{question.questionText} 
        </p:column> 
        <p:column headerText="Question Short Description"> 
         #{question.questionShortText} 
        </p:column> 
        <p:column headerText="Author"> 
         #{question.professor.profSurename} #{question.professor.profName} 
        </p:column> 
       </p:dataTable> 


       <p:dialog header="Question Details" widgetVar="questionDialog" resizable="true" id="questDialog" 
          showEffect="fade" hideEffect="fade" modal="true"> 
        <p:dataTable id="AnswerToQuestionDialogTable" var="answer" value="#{allQuestionBean.answers}"> 

         <f:facet name="header"> 
          Hier kommt der QR_Code rein! 

          #{allQuestionBean.selectedQuestion.questionId} - #{allQuestionBean.selectedQuestion.questionName} 
         </f:facet> 

         <p:column headerText="Answer"> 
          <h:outputText value="#{answer.answerText}"/> 
         </p:column> 
         <p:column headerText="Counts For this Answer"> 
          <h:outputText value="Bis jetz noch nix!"/> 
         </p:column> 
        </p:dataTable> 
       </p:dialog> 


      </h:form> 
     </ui:define> 
    </ui:composition> 

</html> 

및 관련 콩 클래스 (AllQuestionBean.class) :

@ManagedBean(name = "allQuestionBean") 
    @ViewScoped 
    public class AllQuestionBean implements Serializable { 


    private static final long serialVersionUID = 7038894302985973905L; 

    @ManagedProperty(value = "#{questionDAO}") 
    private QuestionDAO questionDAO; 

    @ManagedProperty(value = "#{profSession.professor}") 
    private Professor professor; 

    @ManagedProperty(value = "#{answerDAO}") 
    private AnswerDAO answerDAO; 

    @ManagedProperty(value = "#{answeredDAO}") 
    private AnsweredDAO answeredDAO; 


    private List<Question> questions; 
    private Question selectedQuestion; 
    private List<Answer> answers; 
    private AllQuestionDataHelper allQuestionDataHelper; 


    public AllQuestionBean(){ 
     System.out.println("Starting Bean: "+this.getClass().getName()); 
    } 

    @PostConstruct 
    public void initVariables(){ 
     questions = questionDAO.readByProfessor(professor); 
    } 

    public void onRowSelect(SelectEvent event) { 



     FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName()); 

     FacesContext.getCurrentInstance().addMessage(null, msg); 
    } 

    public void onRowUnselect(UnselectEvent event) { 

     FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName()); 

     FacesContext.getCurrentInstance().addMessage(null, msg); 
    } 


    //---GETTER and SETTER 

    public AllQuestionDataHelper getAllQuestionDataHelper() { 
     allQuestionDataHelper = new AllQuestionDataHelper(questions); 
     return allQuestionDataHelper; 
    } 


    public void setAllQuestionDataHelper(AllQuestionDataHelper allQuestionDataHelper) { 
     this.allQuestionDataHelper = allQuestionDataHelper; 
    } 



    public QuestionDAO getQuestionDAO() { 
     return questionDAO; 
    } 

    public void setQuestionDAO(QuestionDAO questionDAO) { 
     this.questionDAO = questionDAO; 
    } 

    public Professor getProfessor() { 
     return professor; 
    } 

    public void setProfessor(Professor professor) { 
     this.professor = professor; 
    } 

    public AnswerDAO getAnswerDAO() { 
     return answerDAO; 
    } 

    public void setAnswerDAO(AnswerDAO answerDAO) { 
     this.answerDAO = answerDAO; 
    } 

    public AnsweredDAO getAnsweredDAO() { 
     return answeredDAO; 
    } 

    public void setAnsweredDAO(AnsweredDAO answeredDAO) { 
     this.answeredDAO = answeredDAO; 
    } 

    public List<Question> getQuestions() { 
     return questions; 
    } 

    public void setQuestions(List<Question> questions) { 
     this.questions = questions; 
    } 

    public Question getSelectedQuestion() { 
     System.out.println("getSelectedQuestion"); 
     return selectedQuestion; 
    } 

    public void setSelectedQuestion(Question selectedQuestion) { 
     System.out.println("Set selected Question: "+selectedQuestion); 
     this.selectedQuestion = selectedQuestion; 
    } 

    public List<Answer> getAnswers() { 
     answers = answerDAO.getAllAnswersForQuestion(selectedQuestion); 
     return answers; 
    } 

    public void setAnswers(List<Answer> answers) { 
     this.answers = answers; 
    } 
    } 

데이터 MODELL :

public class AllQuestionDataHelper extends ListDataModel<Question> implements SelectableDataModel<Question> { 


    public AllQuestionDataHelper() { 
    } 

    public AllQuestionDataHelper(List<Question> list) { 
     super(list); 
    } 

    @Override 
    public Object getRowKey(Question question) { 
     if(!(question == null)){ 
     System.out.println("Your Questions --> Getting RowKey"); 
     System.out.println("RowKey: "+question); 
     System.out.println("RowKey: "+question.getQuestionId()); 
     }else{ 
      System.out.println("Warning Row Key is null"); 
     } 
     return question.getQuestionId(); 
    } 

    @Override 
    public Question getRowData(String rowKey) { 
     System.out.println("Your Questions --> Getting RowData"); 
     System.out.println("RowData: "+rowKey); 

     List<Question> questionList = (List<Question>) getWrappedData(); 

     for(Question q : questionList){ 
      if(rowKey.equals(q.getQuestionId())){ 
       System.out.println("Returning "+q.getQuestionId()); 
       return q; 
      } 
     } 
     return null; 
    } 
} 

나는 몇 가지 실행을 디버깅 AllQuestionBean.class의 변수 "selectedQuestion"은 절대로 설정되지 않는다고 언급했습니다. "onRowSelect"의 이벤트 변수에 NULL-Object가 들어 있습니다. * .xhtml에는 두 개의 Datatables이 있습니다. 첫 번째는 정상적으로로드되며 아무런 문제가 없습니다. Bean의 onClick-Method는 두 번째 Datatable을 사용하여 대화 상자를 시작해야하지만 Nullpointer를 사용하여 종료합니다.

은 데이터 테이블을 위해 나는 p:dataTable에 (http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionInstant.jsf)는

+3

선택할 데이터 테이블에 rowKey가 필요하지 않습니까? – JamesB

+0

나는 rowKey가 데이터 모델에서 왔다고 생각했다. 한 순간, 모델 소스를 사용하여 질문을 업데이트 할 것입니다. – Chris

+0

좋아, 나는 Nullpointer와 함께 문제를 해결했다. DataModel 클래스의 getRowData() - Method였습니다. 정수와 문자열을 같게하려고했는데,이 시간에 일어났습니다. 좋아, 테이블은 지금 Nullpointer를 던지지 않지만 다른 문제에 직면합니다. 나는 단지 테이블에서 하나의 시간 만 누를 수 있습니다. 다른 모든 클릭에는 표에 아무런 영향이 없습니다. 범위 또는 Ajax에 문제가있을 수 있습니까 ?? – Chris

답변

1

사용 rowKey 속성 Primefaces에서 튜토리얼을 따라 갔다.

rowKey은 Primefaces 엔진이 선택에 따라 선택한 개체를 반환하는 데 도움이되는 고유 한 식별자입니다.

일반적으로 rowKey 속성에 입력하는 값은 p:dataTable으로 채우려는 POJO의 고유 속성입니다.

POJO에 고유 한 필드가없는 경우 그러면 항상 유용하게 쓰일 수 있습니다. 예를 들어 int rowId;입니다. 목록에 추가하는 동안 증가시키고 POJO에 넣을 수 있습니다.

관련 문제