JSF 2 Mojarra - f:ajax 渲染后未遵循必需的属性
考虑这个简单的例子。如果我有这样的表单,那么在 inputText 中没有值的情况下提交时,我显然会收到验证错误:
<h:form id="testForm" prependId="false">
<h:selectOneMenu
id="testSelectOneMenu"
value="#{backButtonTestBean.selection}">
<f:selectItem itemValue="mickey" itemLabel="Mickey" />
<f:selectItem itemValue="mouse" itemLabel="Mouse" />
<f:ajax execute="@this" render="panel" />
</h:selectOneMenu>
<h:panelGroup layout="block" id="panel">
<h:panelGroup layout="block">
<h:inputText id="requiredField" required="true" />
</h:panelGroup>
</h:panelGroup>
<h:commandButton value="submit" action="#{backButtonTestBean.submit}" />
</h:form>
但是当我重新渲染 panelGroup w/ f:ajax 时,所需的属性不会被遵守,我可以使用空值提交:
<h:form id="testForm" prependId="false">
<h:selectOneMenu
id="testSelectOneMenu"
value="#{backButtonTestBean.selection}">
<f:selectItem itemValue="mickey" itemLabel="Mickey" />
<f:selectItem itemValue="mouse" itemLabel="Mouse" />
<f:ajax execute="@this" render="panel" />
</h:selectOneMenu>
<h:panelGroup layout="block" id="panel">
<h:panelGroup layout="block" rendered="#{backButtonTestBean.selection == 'mouse'}">
<h:inputText id="requiredField" required="true" />
</h:panelGroup>
</h:panelGroup>
<h:commandButton value="submit" action="#{backButtonTestBean.submit}" />
</h:form>
我正在 mojarra 2.1.1 中进行测试。任何建议表示赞赏。
Consider this simple example. If I have a form like this I obviously get a validation error when submitting with no value in the inputText:
<h:form id="testForm" prependId="false">
<h:selectOneMenu
id="testSelectOneMenu"
value="#{backButtonTestBean.selection}">
<f:selectItem itemValue="mickey" itemLabel="Mickey" />
<f:selectItem itemValue="mouse" itemLabel="Mouse" />
<f:ajax execute="@this" render="panel" />
</h:selectOneMenu>
<h:panelGroup layout="block" id="panel">
<h:panelGroup layout="block">
<h:inputText id="requiredField" required="true" />
</h:panelGroup>
</h:panelGroup>
<h:commandButton value="submit" action="#{backButtonTestBean.submit}" />
</h:form>
But when I re-render the panelGroup w/ f:ajax the required attribute is not being honored and I can submit with an empty value:
<h:form id="testForm" prependId="false">
<h:selectOneMenu
id="testSelectOneMenu"
value="#{backButtonTestBean.selection}">
<f:selectItem itemValue="mickey" itemLabel="Mickey" />
<f:selectItem itemValue="mouse" itemLabel="Mouse" />
<f:ajax execute="@this" render="panel" />
</h:selectOneMenu>
<h:panelGroup layout="block" id="panel">
<h:panelGroup layout="block" rendered="#{backButtonTestBean.selection == 'mouse'}">
<h:inputText id="requiredField" required="true" />
</h:panelGroup>
</h:panelGroup>
<h:commandButton value="submit" action="#{backButtonTestBean.submit}" />
</h:form>
I'm testing in mojarra 2.1.1. Any advice is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我想通了。 BackButtonTestBean 在请求范围内,因此当我提交表单时 testSelectOneMenu 的值不是“鼠标”。因此它没有被渲染,因此验证器是无效的。更改为 @ViewScoped 并按预期工作。
update: I figured it out. BackButtonTestBean was in request scope so the value of testSelectOneMenu was not 'mouse' when I submitted the form. Consequently it wasn't rendered and thus the validators were invalid. Changed to @ViewScoped and works as expected.