为什么 p:inputTextArea 值没有设置?

Why p:inputTextArea value is not set?

我有这个表格:

<h:form id="productsForm">
 <p:dialog id="newProductDlg">
  <p:panelGrid>
   <p:outputLabel value="Name:"/>
    <p:inputText id="newProductName" value="#{productService.name}" />

    <p:outputLabel value="Category:"/>
    <p:selectOneMenu id="parentCategoryList"
     value="#{productService.category}">
     <f:selectItems var="currCateg" itemLabel="#{currCateg}"
      value="#{categoryService.categories}" itemValue="#{currCateg}" />
    </p:selectOneMenu>

    <p:outputLabel value="Price:"/>
    <p:spinner id="priceSpinner" value="#{productService.price}"/>

    <p:outputLabel value="Specifications:"/>
    <p:commandButton value="Add specifications"
     title="Open 'Add specifications' dialog"/>

    <f:facet name="footer">   
                  <p:commandButton id="addNewProductBtn" value="Add new product"
      process="productsForm:specificationsArea productsForm:newProductDlg"
      actionListener="#{productService.createProduct}"/>
     <p:commandButton id="cancelAddingProd" value="Cancel"/>
    </f:facet>
   </p:panelGrid>
  </p:dialog>

  <p:dialog id="newSpecificationDlg">
   <p:panelGrid>
    <p:outputLabel value="Title:"/>
    <p:inputText id="newSpecificationTitle"/>

    <p:outputLabel value="Description:"/>
    <p:inputText id="newSpecificationDesc"/>

    <f:facet name="footer">
     <p:inputTextarea readonly="true" autoResize="false" 
                        rows="7" id="specificationsArea"
      value="#{productService.specifications}">
     </p:inputTextarea>

     <p:commandButton value="Add new specification entry" id="addNewSpecifEntryBtn"
      update="productsForm:specMessages"
                      process="newSpecificationTitle newSpecificationDesc specMessages"/>

     <p:commandButton id="cancelAddingSpecif" value="Back"
      styleClass="CategDlgBtn dlgField" type="button"/>
    </f:facet>
    </p:panelGrid>
  </p:dialog>
 </h:form>

在这里,textArea 类似于 template,向我们展示值将如何显示在另一个页面上。这就是为什么它是 readonly.

它的工作原理如下:
1) 在主对话框 (newProductDlg) 中,我写了 namecategoryother 参数,
2) 我可以打开第二个对话框 (newSpecificationDlg) 并在那里写新的规范,
3)在newSpecificationDlg中有两个输入newSpecificationTitlenewSpecificationDesc。如果我按 addNewSpecifEntryBtn specificationsArea 的值通过 javascript 附加这两个输入的值:

var title = newSpecificationTitle.value, desc = newSpecificationDesc.value;
if (title == null || desc == null || title.length == 0 || desc.length == 0)
    return;
specificationsArea.value = specificationsArea.value + title + ' : ' + desc
        + ';\n';

在这种情况下 setter for specifications 永远不会被调用。

那么,为什么会发生这种情况以及如何(在我的例子中)将 p:inputTextArea 的值保存到支持 bean 字段?

Primefaces 文档说明了 inputTextArea

readonly 属性

Flag indicating that this component will prevent changes by the user.

这意味着此字段永远不会在客户端上更改,因为永远不会调用 setter。

如果你想使用 p:inputTextArea 来显示文本,你可以将它与你存储相同值的 h:inputHidden 字段结合起来,这个隐藏字段可以在支持 bean 上读取。

XHTML代码:

<p:inputTextarea readonly="true" autoResize="false" 
    rows="7" id="specificationsArea"
    value="#{productService.specifications}">
</p:inputTextarea>
<h:inputHidden id="specificationsHidden" value="#{productService.specificationsHidden}"/>

JS代码:

var title = newSpecificationTitle.value, desc = newSpecificationDesc.value;
if (title == null || desc == null || title.length == 0 || desc.length == 0)
    return;
specificationsArea.value = specificationsArea.value + title + ' : ' + desc
            + ';\n';
specificationsHidden.value = specificationsArea.value + title + ' : ' + desc
            + ';\n';

P.S.: 对不起我的英语水平。