我正在使用基于 SEAM 的 JSF Web 应用程序。我有一个简单的表单,其中包含一个查找(弹出窗口),用于检索类别名称和 ID(两者都可以顺利检索并存储在 myView 中)。当查找返回时,类别名称用于填充“selectedCategoryName”(使用 Jquery)。
当您尝试提交表单,而另一个字段验证失败(简单的必填字段缺失)时,就会出现问题。我的所有其他字段保持不变,但 selectedCategoryName 被清除(即使 bean 仍然具有其值,因此修复验证错误并重新提交可以解决正确的提交)。所以我的问题是,当表单验证失败时,如何保持显示“selectedCategoryName”中的值?
提前致谢!
<s:decorate id="selectedCategoryField" template="/layout/edit.xhtml">
<span>
<h:panelGroup rendered="#{! myView.persisted}">
<img class="lookup" src="${request.contextPath}/images/lookup.gif" width="15" height="14" alt=""
title="Category Lookup"
onclick="return openNewWindow('${CategoryLookupUrl}?#{myView.lookupParameters}', 'id');"/>
</h:panelGroup>
<h:inputText id="selectedCategoryName" required="true"
value="#{myView.selectedCategoryName}"
size="50"
readonly="true">
<a:support event="onchanged" reRender="selectedCategoryField" ajaxSingle="true"/>
<a:support event="oninputchange" reRender="selectedCategoryField" ajaxSingle="true"/>
</h:inputText>
</span>
最佳答案
问题是,实际上,由于 readonly="true"
,bean 并不保存所选值。当您将该属性应用于 UIInput 时,UIInput 将不会在提交时进行处理。
要解决此问题,您可以执行以下操作:
如果您使用 JSF2,则可以使用 readonly="#{facesContext.renderResponse}"
代替。
如果没有,请在支持 bean 上定义一个方法 isReadonly
并使用 readonly="#{myView.isReadonly}"
。
public boolean isReadonly() {
return FacesContext.getCurrentInstance().getRenderResponse();
}
请查看以下类似问题,尤其是答案,以了解有关其工作原理的更多详细信息: Data in <h:inputText readonly="true"> disappears when command button is clicked
关于javascript - 表单验证清除 JavaScript 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470857/