Ui: enable depending on viewParam

I have a page where I want to include part of the page (the footer in this case) depending on the values ​​given from the view parameter.

I have my ViewScoped support for bean initialization on preRenderView

 <f:metadata> <f:viewParam name="racecode" value="#{displayResults.racecode}" /> <f:event type="preRenderView" listener="#{displayResults.init}" /> </f:metadata> 

This queries the database to get the footer name to be included. Then it is used as follows:

 <h:panelGroup id="customFooter" display="block"> <ui:include src="#{displayResults.customFooter}" /> </h:panelGroup> 

This always gives me the missing page. But if I enter the page name manually, it will work. Same thing if I replaced ui:include with h:outputText .

I understand that it has something to do with JSF steps and that, at runtime ui:include value has not yet been set. (reading and a better understanding of the phases is something on my TODO list).

The question remains. How can I get something like this. Ask the bean to use viewParam, query the database and use this value in ui:include ?

+4
source share
2 answers

@wemu already explained the reason. <ui:include src> is evaluated before the init() method is called. However, his proposed solution <f:phaseListener> inconvenient.

Just use @ManagedProperty / @PostConstruct on the @RequestScoped bean.

 @ManagedProperty("#{param.racecode}") private String racecode; @PostConstruct public void init() { // ... } 
+3
source

PreRenderView listeners are called inside the RenderResponsePhase before components are displayed, BUT AFTER the TagGandlers call. This means that TagHandlers will NOT see the data initialized in the PreRenderView event.

If you use <ui:include value="#{myBean.myViewId}" /> to dynamically switch on, you cannot use the PreRenderView event listener to set the myViewId property of myBean .

If you need to do this, use <f:phaseListener> .

+1
source

Source: https://habr.com/ru/post/1437819/


All Articles