anotherUser
anotherUser

Reputation: 561

Primefaces p:cache

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.

Upvotes: 0

Views: 2125

Answers (1)

anotherUser
anotherUser

Reputation: 561

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.

Upvotes: 2

Related Questions