2013-07-24 65 views
0

我試圖在用戶單擊保存按鈕h:commandButton時將編輯的數據保存在t:dataTable對象中。但是,在調用動作之前,將調用bean中的postConstruct()方法,該方法試圖加載表的數據,但沒有最初傳入的docId。我嘗試在h中使用f:param :commandButton傳入docId,但不起作用。有沒有人有正確的策略用docId加載頁面,然後在點擊保存按鈕後保存更改?以下是我目前的bean代碼和xhtml。我不幸選擇升級到JSF 2.0。如何從JSF 1.2中的<t:dataTable>保存表單數據

<h:form enctype="multipart/form-data"> 
    <t:outputText value="Document: #{documentWorkflowCommentsBean.document.name}" /> 
    <br/><br/> 
    <t:dataTable id="commentTable" sortable="false" 
       value="${documentWorkflowCommentsBean.document.workflowComments}" 
     var="comment" 
     styleClass="addmTable"> 
    <t:column styleClass="commentId"> 
     <f:facet name="header"> 
     <h:outputText value="ID" /> 
     </f:facet> 
     <t:outputText value="${comment.commentId}"/>        
    </t:column> 
    <t:column styleClass="cr624_wrap cr624_maxwidth200"> 
     <f:facet name="header"> 
     <h:outputText value="Reviewer" /> 
     </f:facet> 
<t:outputText value="${comment.reviewer}"/>       
    </t:column> 
    <t:column styleClass="charColumn"> 
     <f:facet name="header"> 
     <h:outputText value="Type" /> 
     </f:facet> 
<t:outputText value="${comment.commentType}" 
        rendered="${!documentWorkflowCommentsBean.editComments}"/> 
<t:selectOneListbox id="typeList" title="Choose Comment type" 
          size="1" rendered="${documentWorkflowCommentsBean.editComments}" 
          value="${comment.commentType}"> 
     <f:selectItems value="${documentWorkflowCommentsBean.commentTypes}"/> 
     </t:selectOneListbox>        
    </t:column> 
    <t:column styleClass="cr624_wrap cr624_maxwidth200"> 
     <f:facet name="header"> 
     <h:outputText value="Page" /> 
     </f:facet> 
<t:outputText value="${comment.pageNumber}"/>       
    </t:column> 
    <t:column styleClass="cr624_wrap cr624_maxwidth200"> 
     <f:facet name="header"> 
     <h:outputText value="Section/Paragraph" /> 
     </f:facet> 
     <t:outputText value="${comment.sectionParagraph}"/>       
    </t:column> 
    <t:column styleClass="cr624_wrap cr624_maxwidth200"> 
     <f:facet name="header"> 
     <h:outputText value="Comment/Rationale" /> 
     </f:facet> 
<t:outputText value="${comment.commentRationale}"/>       
    </t:column> 
    <t:column styleClass="cr624_wrap cr624_maxwidth200"> 
     <f:facet name="header"> 
     <h:outputText value="PO Resolution" /> 
     </f:facet> 
     <t:outputText value="${comment.poResolution}" 
        rendered="${!documentWorkflowCommentsBean.editComments}"/> 
<t:inputTextarea id="poResolutionTextArea" value="${comment.poResolution}" 
       rendered="${documentWorkflowCommentsBean.editComments}" 
      rows="3" cols="20"/> 
    </t:column> 
    <t:column styleClass="charColumn"> 
     <f:facet name="header"> 
     <h:outputText value="Decision" /> 
     </f:facet> 
<t:outputText value="${comment.decision}" 
        rendered="${!documentWorkflowCommentsBean.editComments}"/> 
<t:selectOneListbox id="decisionList" title="Choose Decision" 
          size="1" rendered="${documentWorkflowCommentsBean.editComments}" 
          value="${comment.decision}"> 
     <f:selectItems value="${documentWorkflowCommentsBean.commentDecisions}"/> 
     </t:selectOneListbox>       
    </t:column> 
    </t:dataTable>  
    <br/> 
    <h:commandButton value="Save" action="#{documentWorkflowCommentsBean.saveDocumentComments}"> 
    <f:param name="docId" value="#{documentWorkflowCommentsBean.documentId"/> 
    <f:param name="editComments" value="#{documentWorkflowCommentsBean.editComments}"/> 
    </h:commandButton> 
    <input type="button" value="Cancel" title="Close the dialog" onclick="closeModal();"/> 
</h:form> 

public class DocumentWorkflowCommentsBean extends PageBean { 
    private static final long serialVersionUID = -866249792018248429L; 
    private static final Logger log = LogManager.getLogger(DocumentWorkflowCommentsBean.class); 

    /** 
    * Holds a reference to the DocumentBusiness object. 
    * 
    * @uml.property name="docBiz" 
    */ 
    private DocumentBusiness docBiz; 

    /** 
    * This represents the documentId parameter passed 
    * 
    * @uml.property name="documentId" 
    */ 
    private long documentId; 

    /** 
    * This is the corresponding Document object represented by the documentId property 
    * 
    * @uml.property name="document" 
    */ 
    private Document document; 

    /** 
    * Determines if the Type, Resolution, and Decision fields are editable 
    * 
    * @uml.property name="editComments" 
    */ 
    private boolean editComments = false; 

    private static final List<SelectItem> COMMENT_TYPES = Arrays.asList(new SelectItem("C", "C"), 
     new SelectItem("M", "M"), 
     new SelectItem("S", "S"), 
     new SelectItem("A", "A")); 

    private static final List<SelectItem> COMMENT_DECISIONS = Arrays.asList(
     new SelectItem("A", "A"), 
     new SelectItem("R", "R"), 
     new SelectItem("M", "M")); 

    /** 
    * This is called after all resources are injected 
    */ 
    @PostConstruct 
    public void postConstruct() { 
    docBiz = BusinessUtils.getDocumentBusiness(); 

    // Get the parameters that are passed in 
    String docIdString = (String) getPassedParam("docId"); 
    String editString = (String) getPassedParam("editComments"); 

    // editComments will be null when closing dialog 
    if (editString != null) { 
     editComments = Boolean.parseBoolean(editString); 
    } 

    if (docIdString != null) { 
     try { 
     // Retrieve the Document object 
    documentId = Long.parseLong(docIdString); 
} catch (NumberFormatException ignore) { 
     // do nothing 
    log.error("Invalid parameter - " + docIdString); 
} 

     if (documentId > 0) { 
     //lazy load of workflow comments to be displayed 
    document = docBiz.getDocumentFetchWorkflowComments(documentId); 
} 

// Check to see that the Document exists 
if (document == null) { 
    this.getAddmSessionBean().addPageErrorMessage("Cannot perform action - document has been deleted."); 
} 
    } 
    } 

    public String saveDocumentComments() { 
    docBiz.updateDocument(document); //JPA merge call on document 
    return null; 
    } 

    public long getDocumentId() { 
    return documentId; 
    } 

    public void setDocumentId(long documentId) { 
    this.documentId = documentId; 
    } 

    public Document getDocument() { 
    return document; 
    } 

    public void setDocument(Document document) { 
    this.document = document; 
    } 

    public List<SelectItem> getCommentTypes() { 
    return COMMENT_TYPES; 
    } 

    public List<SelectItem> getCommentDecisions() { 
    return COMMENT_DECISIONS; 
    } 
} 

回答

0

正如你顯然已經在使用戰斧,你可以使用<t:saveState>模擬JSF 2.x的視圖範圍。

將這個地方的觀點做一個JSF 1.x的請求範圍豆表現得像一個JSF 2.x的視圖範圍的bean:

<t:saveState value="#{documentWorkflowCommentsBean}" /> 

這樣,只要你互動的bean將活通過返回void/null的回發獲得相同的視圖,並且在每次回發時都不會重構/重新初始化,就像在JSF 2.x中一樣。