文档加载页面上的JSF 2.0部分呈现 [英] JSF 2.0 partial rendering on page on document load
问题描述
我有一个页面,仅在整个页面加载后才想加载其中的一部分.为此,我在页面上使用以下代码:
I have a page, one section of which I want to load only after the whole page is loaded. For this, I'm using the following code on the page:
<script type="text/javascript">
alert("In the JS");
$(document).ready(
function() {
$.post(jspPath+'/myCommunity/ajax/hotTopicDiscussion.faces',
function(data) {
jsf.ajax.request(this, 'post', {render: 'discussionTopic'});
}
);
}
);
</script>
还
<h:panelGroup id="discussionTopic" reRendered="#{discussionBean.doRender}">
这个doRender
是我在AJAX调用完成后设置的bean属性.但这不是加载数据.我可以看到ajax方法被调用了.
This doRender
is a bean property which I set once the AJAX call is done. But this is not loading the data. I can see the ajax method is getting called.
推荐答案
您真的真的需要提交表格吗?这就是jsf.ajax.request()
的用途.似乎您只想在<div>
中加载一些静态内容.只需执行以下操作
Do you really need to submit a form ajaxically? That's where the jsf.ajax.request()
is for. It seems like that you just want to load some static content inside some <div>
. Just do as follows
<script>
$(document).ready(function() {
$.get("#{request.contextPath}/myCommunity/ajax/hotTopicDiscussion.faces", function(data) {
$("#discussionTopic").html(data);
});
});
</script>
<h:panelGroup id="discussionTopic" layout="block" />
或更简单,使用 jQuery.load()
:
Or, simpler, with jQuery.load()
:
<script>
$(document).ready(function() {
$("#discussionTopic").load("#{request.contextPath}/myCommunity/ajax/hotTopicDiscussion.faces");
});
</script>
<h:panelGroup id="discussionTopic" layout="block" />
在两种情况下,我都假定hotTopicDiscussion.xhtml
具有<ui:composition>
作为根元素,并且它仅包含必要的HTML(因此不包含<html>
等).
In both cases, I assume that the hotTopicDiscussion.xhtml
has <ui:composition>
as root element and that it contains only the necessary HTML (and thus not <html>
and so on).
如果您确实确实需要真正提交表格,那么最简单的方法就是让JS以编程方式单击"按钮.它将一直进行委派,直到进行正确且需要的jsf.ajax.request()
调用.如果页面上没有物理表单/按钮组件,则jsf.ajax.request()
调用将毫无用处.您可以根据需要通过CSS隐藏表单.
If you indeed really need to submit a form ajaxically, then simplest would be to let JS "click" the button programmatically. It will delegate all the way up to the proper and needed jsf.ajax.request()
call. A jsf.ajax.request()
call is useless anyway without a physical form/button component on the page. You can if necessary hide the form by CSS.
<script>
$(document).ready(function() {
$("[id='form:button']")[0].onclick();
});
</script>
<h:form id="form" style="display:none;">
<h:commandButton id="button" action="#{bean.submit}">
<f:ajax render=":discussionTopic" />
</h:commandButton>
</h:form>
<h:panelGroup id="discussionTopic" layout="block" />
这篇关于文档加载页面上的JSF 2.0部分呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!