2014-02-21 46 views
0

我试图在我的应用程序中实现primefaces的RemoteCommand标记,但不知怎的,它不像文档所说的那样工作。JSF&Primefaces - RemoteCommand工作不正常

我想要实现的是:我有几个页面布局不同,但都使用相同的JSF模板。 (基本上模板的内容发生变化)。不同的页面应该一个接一个地自动加载,并且彼此间隔一段时间。

<p:remoteCommand name="remoteSwitchPage" actionListener="#{circuitBean.redirect()}" autoRun="false"/> 

这是我的JSF代码,这should一个名为remoteSwitchPage() JavaScript方法提供了我,但它不!它也应该not运行,因为我已经说过autoRun="false",但是它运行在加载的时候调用方法remoteSwitchPage()后,它应该通过Java渲染另一个页面(这实际上是工作的,不知何故,当离开括号时,JSF不会找到重定向方法,这就是为什么我放了它们)。

所以总结:

  1. JavaScript不与remoteSwitchPage()
  2. 的remoteCommand在页面加载,它should not!
  3. JSF将不会离开的时候找到我ManagedBean方法resize(),运行提供了我括号

你们有什么线索我在做什么错在这里?

我使用primefaces 4.0JSF 2.1

回答

1

在Simegos'发表的评论说,解决

管理它。

我创建了一个隐藏按钮,每隔X秒用JavaScript点击一次。点击按钮后呈现的每个页面都需要实现以下代码。将代码放在所有渲染页面将实现的模板中是理想的。

<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> 
+1

我很高兴听到你修好它:)好的工作,你也可以标记你的答案为正确的或我的,如果你想,不要让这个问题作为未答复 – Simego

+1

将做它明天;) – JustBasti

3

如果你使用:

<p:remoteCommand name="remoteSwitchPage" actionListener="#{circuitBean.redirect}" /> 

JSF将尝试找到getRedirect,这就是为什么你应该使用circuitBean.redirect()

编辑:我认为你应该使用action而不是actionListener

这是我的示例代码(测试):

HTML:

<a href="#" onclick="logoutAccount()"><i class="fa fa-sign-out fa-fw"></i> Logout</a> 

HTML(页面的端)的另一部分:

<h:form prependId="false"> 
    <p:remoteCommand action="#{sessionMB.logout()}" name="logoutAccount" /> 
</h:form> 

豆:

public void logout() { 
    account = null; 
    logged = false; 
    FacesUtil.addInfoGrowl(MessageProvider.getMessage("message.logout.success"), null); 
    FacesUtil.redirectToPage("", true); 
} 

redirectToPage方法:

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; 
    } 
} 

那么,会是这样:
点击>通话remoteCommand的Javascript >调用Bean >重定向到另一页

我只是没有得到你说的模板部分,如果我能帮助更多的东西告诉我,希望你修复它。

+0

感谢您的范例代码。是否可以在X秒后以编程方式调用'logoutAccount()',而不需要用户点击它? **编辑:**请参阅,这就是为什么我想要在普通JavaScript中使用该方法() – JustBasti

+0

我能想到的唯一解决方案是创建一个不可见的按钮,该按钮将通过一些JavaScript – JustBasti

+0

setTimeout()在文档加载 – VeenarM