How to NOT initialize beans on page load associated with conditionally rendered content

旧巷老猫 提交于 2019-12-08 09:41:18

问题


I have a single page with conditionally rendered content controlled by several links in a sidemenu. All of that content has a datatable that is rendered false by default. Everything is working, except that when the page loads all of the beans are being executed and all beans are doing expensive database work.

I would like that when a sidemenu is clicked by user, the corresponding datatable will render and only then the associated managed bean will be initialized. How can I achieve this?


回答1:


You have basically two options:

  1. Don't navigate by ajax. Instead, navigate by GET. See also How to navigate in JSF? How to make URL reflect current page (and not previous one).

  2. Or, don't conditionally render content. Instead, conditionally build content. See also How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)




回答2:


I recently encountered the such problem. I decided to use a same condition which I use to decide render or not render this or that component to decide initialize or not initialize its backing variables. For example you have JSF page with a code:

<h:outputText rendered="#{bean.isRendered()}" value="#{bean.text}"/>

and bean is following:

@PostConstruct
public void init(){
     if(isRendered){
           text = "Hello World";
     }
}


来源:https://stackoverflow.com/questions/28802881/how-to-not-initialize-beans-on-page-load-associated-with-conditionally-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!