0

I am developing a CMS using JSF 2.3. I need to pass a GET parameter to every page indicating the site that the user is managing. To do this I am using <f:viewParam> in all pages, but I have the following doubts:

  1. Is it OK to use multiple <f:viewAction> for multiple managed beans like the following example?
<f:metadata>
    <f:viewParam name="form" value="#{editFormWebBean.formIdParam}"/>
    <f:viewParam name="site" value="#{headerWebBean.siteIdParam}"/>
    <f:viewAction action="#{editFormWebBean.init}" />
    <f:viewAction action="#{headerWebBean.init}" />
</f:metadata>

This works, but I am not sure if it is OK.

  1. Is there a way to avoid replicating in every page the <f:viewParam> for the site parameter? I tried with includeViewParams, but doesn't worked if I don't include the <f:viewParam> in the source and destination page (from page1.xhtml to page2.xhtml)

3) Can I define multiple <f:metadata> tags? For example if I am using templates and multiple ManagedBeans pare page (one for the header, one for the menu and so on).

Thank you and sorry about my english.

Jucaalpa
  • 310
  • 1
  • 4
  • 15

1 Answers1

0
  1. it's okay to have as many <f:viewParam> and <f:viewAction> as long as they work well together, but please make sure these are meant to initialize the view "JSF page" not the backing beans, use @PostConstruct on the baking beans to initialize them, also but in your mind that <f:viewAction> is only executed on GET request by default any subsequent POST (postback) requests don't invoke the action unless it has onPostBack="true" attribute. more on these tags can be found in this great answer [What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?][1]

  2. put it in a template using the templating feature of JSF like this:

template.xhtml

<f:metadata>
   <f:viewParam name="site" value="#{headerWebBean.siteIdParam}"/>
   <ui:insert name="metadata"/>
</f:metadata>

page.xhtml

<ui:composition template="template.xhtml">
    <ui:define name="metadata">
        <!-- whatever metadata you want to add-->
    </ui:define>
 </ui:comosition>
  1. no it's one metadata tag per page, use it like the example above. [1]: https://stackoverflow.com/a/6377957/4649524
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
alibttb
  • 526
  • 4
  • 19
  • 1: totally depends on what your definition of 'initialize' is. – Kukeltje Apr 19 '19 at 14:45
  • collecting request parameters and get objects depending on these parameters @Kukeltje – alibttb Apr 19 '19 at 14:57
  • And that is done in backing beans right? Beans backing the page so you initialize the page by populating (=sort of initializing) the beans. So I fail to see this statement: _"but please make sure these are meant to initialize the view "JSF page" not the backing beans, "_ Not saying you are wrong, just fail to see what is actually meant or why – Kukeltje Apr 19 '19 at 20:33
  • I tend to separate my view needed objects from other needed objects that may provide service to the view, this can be done in many ways, but in some backing bean you can have both, let's say you get an id and you want the entity this is a view intialization, but if you want to load some file content or read a system property this is a bean init. – alibttb Apr 20 '19 at 08:29
  • main line between the two is the dependency on a view parameter, for me I'll have all my initialization in the PostConstruct method of the backing bean, but the view params aren't available for that method thus I use a view action in some case and this is tge separation. @kukeltje – alibttb Apr 20 '19 at 08:31