0

I'm having some problem with Primefaces p:cache component

This is example how its used on testpage/index.xhtml

<h:form>
<p:panel header="Testsite">
    <p:cache region="testsite2"
        key="testsite2#{user.id}#{user.defaultLanguage}">
        <p:commandButton action="#{testBean.hello}" value="btn" 
             rendered="#{testBean.renderedButton}">                 
        </p:commandButton>
    </p:cache>
</p:panel>
</h:form>

and this is back end bean

@ManagedBean(name = "testBean")
@ViewScoped
public class TestBean {
    @PostConstruct
    public void init() {
        System.out.println("init");
    }
    public void hello() {
        System.out.println("hello");
    }  
    public boolean isRenderedButton() {
        System.out.println("isRenderedButton");
        return true;
    }
}

So on first page hit init and isRenderedButton message are printed normally as expected. After that when I click on button I do expect to see hello message printed, but that's not case here. Can anyone point me in right direction ?

According to Primefaces showcase for p:cache with buttons I was expecting this behavior.

Right now I am using Primefaces.DEFAULT_CHACHE_PROVIDER and later I will switch to ehcache.

I'm using PF 5.3, sun faces 2.2.12.

Thanks.

3
  • did you find the solution
    – misman
    Commented Aug 24, 2016 at 7:56
  • @mismanc no. I have plans (in one or two weeks) to work again on this problem and maybe ill find some answers. It looks to me that this component can only be used in case you have some 'static' content on page, something like sidebar or navigation menu which are used to navigate via links. Not sure. Feel free to comment/post answer if you find something useful. Commented Aug 24, 2016 at 8:45
  • if you dont have to use p:commandButton you may try h:commandButton with omnifaces cache component most probable it will work. I have to use primefaces ajax call so, it not an option for me
    – misman
    Commented Aug 24, 2016 at 9:22

1 Answer 1

2

To answer myself (and maybe ill help someone), i was trying to create dynamic menu from database and i wanted to cache generated content with p:cache component. But back then every menu item would call bean method which would redirect user to page and that was problem in first place. So i had something like this:

<p:menu>
  <p:submenu label="Human resource">
     <p:menuitem value="Search person" actionListener="#{bean.navigateUserToSearchPerson}"/>
  </p:submenu>

I actually did not fix this problem (had no extra time to investigate problem), so i came up with idea to generate links for each menu item, so when user clicks on menu item, it would redirect him to new page. So now i don't have any AJAX calls in menu and caching works fine now. Code example:

<p:cache region="appUiCache" key="panelMenu#{user.id}#{user.defaultLanguage}">
  <p:panelMenu id="sm" model="#{bean.menuModel}" stateful="true" />
</p:cache>

Menu items are created dynamically from database:

DefaultMenuItem defaultMenuItem = new DefaultMenuItem(...);
defaultMenuItem.setIcon(item.getIcon());
defaultMenuItem.setUrl(item.getUrl()); <!-- This is new url part -->

This works fine now in production. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.