我有
<h:form>
<h:commandLink action="#{my_fake_ajax_link}">
<h:outputText value="Link" />
<f:ajax render=":mydiv" />
</h:commandLink>
</h:form>
<h:panelGroup layout="block" id="mydiv">
<h:form>
<h:commandLink action="#{mybean.delete(0)}">
<h:outputText value="Here" />
<f:ajax render="@form" />
</h:commandLink>
</h:form>
</h:panelGroup>
当我在" my_fake_ajax_link"上单击一次时,则必须在"删除"链接上单击两次.这仅仅是一个例子.我没有这个真实的案例.我在页面上有多种表单,不能仅仅将所有表单添加为一个表单。
我检查了问题所在,并且是:
- When you click on "my_fake_ajax_link", the
mydiv
使用应有的ajax刷新。 - 在ajax上刷新的表单的ViewState丢失。
如何添加ViewState? 如何不使用一种表格就可以工作? 在我看来,这似乎是一个JSF错误.我不想使用
自动刷新该div
jQuery("#mydiv").load(document.location.href);
但在最坏的情况下,我会尽可能的。
最新回答
- 2021-1-121 #
- 2021-1-122 #
解决方法:
首先,您需要设置一个
onevent
发送ajax请求的按钮的处理程序:<h:form id="newComment"> <h:commandButton id="saveNewComment" action="#{postBean.actionSaveNewCommentAjax}" value="#{rb['speichern']}"> <f:ajax execute="@form" render=":commentsBox" onevent="function(data) { fixOtherFormsViewState(data, 'newComment') }" /> </h:commandButton> </h:form>
然后,您需要声明此javascript方法,该方法将采用正确的viewState值并将其添加到所有没有它的表单中:
<h:outputScript> function fixOtherFormsViewState(data, goodFormId) { if (data.status != "success") { return; } var viewState = jQuery("#" + goodFormId + " input[name='javax.faces.ViewState']").val(); jQuery("form:not(:contains(input[name='javax.faces.ViewState']))").each(function (idx, elem) { var form = jQuery(elem); var input = jQuery("<input type='hidden' name='javax.faces.ViewState' />").val(viewState); form.append(input); }); } </h:outputScript>
这是自动随附的
jsf.js
中的一个已知问题 处理ajax响应的JSF库.另请参见即将发布的JSF 2.3中已修复的JSF规范问题790.同时,在JSF 2.0 / 2.1 / 2.2中,您必须明确指定另一个<h:form>
的ID。 在render
内 触发适当添加视图状态的属性。否,这不会在ajax响应中引起任何开销或标记重复.或者,使用OmniFaces
另请参见:fixviewstate.js
Communication in JSF 2.0 - Ajax rendering of content which contains another form
h:commandButton/h:commandLink does not work on first click, works only on second click