如何从JSF 1.2 </t:datatable>中的<t:datatable>保存表单数据

时间:2013-07-24 18:14:19

标签: forms datatable save jsf-1.2 postconstruct

当用户点击“保存”按钮h:commandButton时,我正在尝试保存在t:dataTable对象中编辑的数据。但是,在调用该操作之前,调用bean中的postConstruct()方法,该方法尝试加载表的数据,但没有最初传入的docId。我尝试在h中使用f:param :commandButton传入docId,但这不起作用。有没有人有正确的策略来加载带有docId的页面,然后在单击保存按钮后保存更改?以下是我当前的bean代码和xhtml。不幸的是,我没有选择升级到JSF 2.0。

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

1 个答案:

答案 0 :(得分:0)

由于您显然已经在使用Tomahawk,因此您可以使用<t:saveState>模拟JSF 2.x视图范围。

将它放在视图中的某个位置,使JSF 1.x请求作用域的bean表现得像JSF 2.x视图范围的bean:

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

这样,只要您通过返回void / null的回发与相同的视图进行交互,bean就会生存,并且不会在每次回发时重建/重新初始化,就像在JSF中一样2.X。