Java JSF 和 Primefaces - RemoteCommand 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21942116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF & Primefaces - RemoteCommand not working properly
提问by Sebastian
I'm trying to implement primefaces' RemoteCommand tag to my application, but somehow it doesn't work as the docs say.
我正在尝试在我的应用程序中实现 primefaces 的 RemoteCommand 标记,但不知何故它不像文档所说的那样工作。
What I'm trying to implement:I have a several pages with different layouts, but all are using the same JSF Template. (Basically the content of the template changes). The different pages should load automatically though, one after another, one by one, with an interval between one another.
我正在尝试实现的内容:我有几个具有不同布局的页面,但都使用相同的 JSF 模板。(基本上是模板的内容发生了变化)。不同的页面应该自动加载,一个接一个,一个接一个,彼此之间有间隔。
<p:remoteCommand name="remoteSwitchPage" actionListener="#{circuitBean.redirect()}" autoRun="false"/>
This is my JSF code, which should
supply me with a JavaScript method called remoteSwitchPage()
, but it does not!It should also not
run, because I've said autoRun="false"
, but it runs on load!After calling the method remoteSwitchPage()
it should render another page via Java (This one works actually. Somehow JSF wouldn't find the redirect method when leaving the brackets, that's why I've put them).
这是我的 JSF 代码,它should
为我提供了一个名为 的 JavaScript 方法remoteSwitchPage()
,但它没有!它也应该not
运行,因为我已经说过autoRun="false"
,但它在负载上运行!调用该方法后,remoteSwitchPage()
它应该通过 Java 呈现另一个页面(这实际上是有效的。不知何故,JSF 在离开括号时找不到重定向方法,这就是我放置它们的原因)。
So summarized:
总结如下:
- JavaScript does not supply me with the
remoteSwitchPage()
- The remoteCommand runs on page load, which it
should not!
- JSF won't find my ManagedBean method
resize()
, when leaving the brackets
- JavaScript 没有为我提供
remoteSwitchPage()
- remoteCommand 在页面加载时运行,它
should not!
resize()
离开括号时,JSF 将找不到我的 ManagedBean 方法
Do you guys got any clue what I am doing wrong here?
你们知道我在这里做错了什么吗?
I'm using primefaces 4.0
and JSF 2.1
我正在使用primefaces 4.0
和JSF 2.1
采纳答案by Sebastian
SOLVED
解决了
Managed it as said in the comment of Simegos' Post.
正如 Simegos 帖子的评论中所说的那样管理它。
I created a hidden button that would be clicked every X seconds by JavaScript. Every page that becomes rendered after the button got clicked, needs to implement the following code. It would be ideal to put the code in a template which all rendered pages will implement.
我创建了一个隐藏按钮,JavaScript 每 X 秒点击一次。单击按钮后呈现的每个页面都需要实现以下代码。将代码放在所有渲染页面都将实现的模板中是理想的。
<h:form id="form" class="hidden"> --> Hidden button
<h:commandButton id="switchPageButton" action="#{circuitBean.renderNextScreen()}"/>
</h:form>
<h:outputScript>
$(function() {
$(".hidden").each(function() { --> Declare Form as hidden, since it becomes overwritten by JSF
$(this).css("display","none");
});
var switchPageLink = $("#form\:switchPageButton"); --> Get Button via ID
window.setInterval(function() {
switchPageLink.click(); --> Click it
}, #{circuitBean.pageInterval}); --> after X seconds. I'm reading the interval through the bean here. You can just put a number in there
});
</h:outputScript>
回答by Simego
if you use:
如果您使用:
<p:remoteCommand name="remoteSwitchPage" actionListener="#{circuitBean.redirect}" />
JSF will try to find getRedirect
, that's why you should use the circuitBean.redirect()
.
JSF 将尝试查找getRedirect
,这就是您应该使用circuitBean.redirect()
.
EDIT: i think you should use action
instead of actionListener
.
编辑:我认为你应该使用action
而不是actionListener
.
Here is my example code (tested):
这是我的示例代码(已测试):
HTML:
HTML:
<a href="#" onclick="logoutAccount()"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
Another part of HTML (end of the page):
HTML 的另一部分(页面末尾):
<h:form prependId="false">
<p:remoteCommand action="#{sessionMB.logout()}" name="logoutAccount" />
</h:form>
Bean:
豆角,扁豆:
public void logout() {
account = null;
logged = false;
FacesUtil.addInfoGrowl(MessageProvider.getMessage("message.logout.success"), null);
FacesUtil.redirectToPage("", true);
}
redirectToPage method:
重定向到页面方法:
public static boolean redirectToPage(String completePath, Boolean keepMessages) {
try {
FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(keepMessages);
FacesContext.getCurrentInstance().getExternalContext().redirect(SimegoUtil.redirectProject() + completePath);
return true;
} catch (IOException ex) {
FacesUtil.addErrorGrowl(MessageProvider.getMessage("message.redirectFail"), null);
return false;
}
}
So, will be like this:
Click >call remoteCommand Javascript >call bean >redirect to another page
所以,会是这样:
点击>调用 remoteCommand Javascript >调用 bean >重定向到另一个页面
I just didn't get the template part you said, if i can help in something more just tell me, hope you fix it.
我只是没有得到你说的模板部分,如果我能提供更多帮助,请告诉我,希望你能修复它。