Quantcast
Channel: OmniFaces & JSF Fans
Viewing all articles
Browse latest Browse all 74

[OmniFaces utilities (2.4)] Get all request query string or view parameters appended with all child UIParameter components of the given parent component

$
0
0

[OmniFaces utilities] The getParams() returns an unmodifiable map with all request query string or view parameters, appended with all child UIParameter components (<f|o:param>) of the given parent component. Those with disabled=true or an empty name or an empty value are skipped. The <f|o:param> will override any included view or request parameters on the same name.

Method:
Usage:

Let's suppose that we have the following page:

<f:metadata>
 <f:viewParam name="playernameparam" value="#{playersBean.playerName}"/>           
 <f:viewParam name="playersurnameparam" value="#{playersBean.playerSurname}"/>
</f:metadata>
<h:head>
 <title></title>
</h:head>  
<h:body>    
 <h:panelGrid columns="2">
  <h:form id="form">
   <h:commandButton id="submit" value="Get all params" action="#{playersBean.allParams()}">   
    <f:param name="ageParam" value="29"/>
    <o:param name="playParam" value="left"/>
   </h:commandButton>
  </h:form>                 
 </h:panelGrid>
</h:body>

Further, we can use the Components#getParams()utility to programmatically collect the UIParameters  (<f|o:param/>) nested in the above <h:commandButton/> and the view parameters. This can be accomplish in different JSF artifacts; for example, in a bean (obviously, there are several ways to identify the parent UIComponentof the UIParameters that we want to collect - the UIComponentthat should be passed to the #getParams()should be obtained in a convenient manner depending on your circumstances; below, we simply used the Components#findComponent()utility):

import org.omnifaces.util.Components;
...
public void allParams() {
              
 UIComponent uc = Components.findComponent("form:submit");
 Map<String, List<String>> paramsMap = Components.getParams(uc, false, true);

 for (Map.Entry<String, List<String>> entry : paramsMap.entrySet()) {
      System.out.println("Param: "+ entry.getKey() + "/" + entry.getValue());
 }
}

Let's suppose that we start the application with an URL like this:

http://myapp/?playernameparam=rafael&playersurnameparam=nadal

After we press the Get all params button the server log will reveal the following output:

Param: playernameparam/[rafael]
Param: playersurnameparam/[nadal]
Param: ageParam/[29]
Param: playParam/[left]


Viewing all articles
Browse latest Browse all 74

Trending Articles