admin管理员组

文章数量:1334374

I'm trying to implement primefaces' RemoteCommand tag to my application, but somehow it doesn't work as the docs say.

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.

<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).

So summarized:

  1. JavaScript does not supply me with the remoteSwitchPage()
  2. The remoteCommand runs on page load, which it should not!
  3. JSF won't find my ManagedBean method resize(), when leaving the brackets

Do you guys got any clue what I am doing wrong here?

I'm using primefaces 4.0 and JSF 2.1

I'm trying to implement primefaces' RemoteCommand tag to my application, but somehow it doesn't work as the docs say.

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.

<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).

So summarized:

  1. JavaScript does not supply me with the remoteSwitchPage()
  2. The remoteCommand runs on page load, which it should not!
  3. JSF won't find my ManagedBean method resize(), when leaving the brackets

Do you guys got any clue what I am doing wrong here?

I'm using primefaces 4.0 and JSF 2.1

Share asked Feb 21, 2014 at 18:15 SebastianSebastian 1,6544 gold badges28 silver badges43 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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().

EDIT: i think you should use action instead of actionListener.

Here is my example code (tested):

HTML:

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

Another part of HTML (end of the page):

<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 pletePath, Boolean keepMessages) {
    try {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(keepMessages);
        FacesContext.getCurrentInstance().getExternalContext().redirect(SimegoUtil.redirectProject() + pletePath);

        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

I just didn't get the template part you said, if i can help in something more just tell me, hope you fix it.

SOLVED

Managed it as said in the ment of Simegos' Post.

I created a hidden button that would be clicked every X seconds by JavaScript. Every page that bees 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.

<h:form id="form" class="hidden"> --> Hidden button
    <h:mandButton id="switchPageButton" action="#{circuitBean.renderNextScreen()}"/>
</h:form>    
<h:outputScript>
    $(function() {
        $(".hidden").each(function() { --> Declare Form as hidden, since it bees 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> 

本文标签: javaJSF amp PrimefacesRemoteCommand not working properlyStack Overflow