javascript - 为什么 p :inputTextArea value is not set?

标签 javascript java jsf-2 primefaces

我有这个表格:

<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,它向我们展示了值如何在另一个页面上显示。这就是为什么它是只读

其工作原理如下:
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

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

最佳答案

Primefaces 文档介绍了 inputTextAreareadonly 属性

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

PS:抱歉我的英语水平。

关于javascript - 为什么 p :inputTextArea value is not set?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230330/

相关文章:

javascript - 在这些条件下使用 Javascript 或 jquery 滚动到页面底部

javascript - 在 href 属性中过滤 XSS?

java - Java 中的 'this' 关键字

javascript - 检查是否加载了内联 SVG 图像

javascript - 检测两个动画 div 的碰撞

java - 使用 objectInputStream 读取时线程 "main"java.io.EOFException 中出现异常

java - 为什么会出现编译错误 "org/codehaus/groovy/control/CompilationFailedException"?

jsf - 带子表和多项选择的 Primefaces 数据表

jsf - 如何动态构建back bean编辑表单

JSF 2 : selection grouping with SelectItemGroup + POJO