f:param

f:param Facelet sends a named parameter value if used in any commandButton, commandLink, outputLink or button. These parameters will be sent as form parameters when doing the POST/GET requests. In order to get them, you can use ExternalContext getRequestParameterMap method for getting the sent value.

It's important to notice the differences between f:param and f:attribute:
  • f:attribute stores the key-values in the component itself, so, in order to get those attributes, the reference to the UIComponent should be available. For getting the f:param values is enough to use the getRequestParameterMap method mentioned before.
  • f:param should be used for sending String parameter values. If it's intented to use Object, then use f:attribute instead

General Usage

Source Code

  1. <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core"
  2. xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
  3.  
  4. <h:form>
  5. <h:messages id="feedback" globalOnly="true" layout="table" />
  6. <h:commandLink actionListener="#{commandBackingBean.parameterActionListener}" value="#{i18n['link']}">
  7. <f:ajax execute="@form" render="feedback" />
  8. <f:param name="parameter" value="12345"/>
  9. </h:commandLink>
  10. </h:form>
  11.  
  12. </ui:composition>
  1. @ManagedBean
  2. @RequestScoped
  3. public class CommandBackingBean {
  4.  
  5. private static final Logger logger = LoggerFactory.getLogger(CommandBackingBean.class);
  6.  
  7. // Injections
  8. @ManagedProperty(value = "#{commandModelBean}")
  9. private CommandModelBean commandModelBean;
  10.  
  11. public void actionListener(ActionEvent actionEvent) {
  12.  
  13. FacesContext facesContext = FacesContext.getCurrentInstance();
  14. PhaseId phaseId = facesContext.getCurrentPhaseId();
  15. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  16.  
  17. String phaseName = phaseId.toString();
  18. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  19. " phase of the JSF lifecycle.");
  20. facesContext.addMessage(null, facesMessage);
  21. }
  22.  
  23. public void ajaxListener(AjaxBehaviorEvent ajaxBehaviorEvent) {
  24.  
  25. FacesContext facesContext = FacesContext.getCurrentInstance();
  26. PhaseId phaseId = facesContext.getCurrentPhaseId();
  27. logger.debug("ajaxListener: phaseId=[{0}]", phaseId.toString());
  28.  
  29. String phaseName = phaseId.toString();
  30. FacesMessage facesMessage = new FacesMessage("The ajaxListener method was called during the " + phaseName +
  31. " phase of the JSF lifecycle.");
  32. facesContext.addMessage(null, facesMessage);
  33. }
  34.  
  35. public void attributeActionListener(ActionEvent actionEvent) {
  36.  
  37. FacesContext facesContext = FacesContext.getCurrentInstance();
  38. PhaseId phaseId = facesContext.getCurrentPhaseId();
  39. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  40.  
  41. String value = (String) actionEvent.getComponent().getAttributes().get("attribute");
  42.  
  43. String phaseName = phaseId.toString();
  44. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  45. " phase of the JSF lifecycle.The attribute value is " + value);
  46. facesContext.addMessage(null, facesMessage);
  47. }
  48.  
  49. public void attributesActionListener(ActionEvent actionEvent) {
  50.  
  51. FacesContext facesContext = FacesContext.getCurrentInstance();
  52. PhaseId phaseId = facesContext.getCurrentPhaseId();
  53. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  54.  
  55. Map<String, Object> attributes = actionEvent.getComponent().getAttributes();
  56. String value1 = (String) attributes.get("attribute1");
  57. String value2 = (String) attributes.get("attribute2");
  58.  
  59. String phaseName = phaseId.toString();
  60. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  61. " phase of the JSF lifecycle.The attributes value are " + value1 + " and " + value2);
  62. facesContext.addMessage(null, facesMessage);
  63. }
  64.  
  65. public void feedbackListener(ActionEvent actionEvent) {
  66.  
  67. String value = "";
  68.  
  69. List<UIComponent> children = actionEvent.getComponent().getChildren();
  70.  
  71. for (UIComponent uiComponent : children) {
  72.  
  73. if (uiComponent instanceof UIOutput) {
  74. value = (String) ((UIOutput) uiComponent).getValue();
  75. }
  76. }
  77.  
  78. FacesContext facesContext = FacesContext.getCurrentInstance();
  79. logger.debug("feedbackListener: You selected the '" + value + "' menu item.");
  80.  
  81. FacesMessage facesMessage = new FacesMessage("You selected the '" + value + "' menu item.");
  82. facesContext.addMessage(null, facesMessage);
  83. }
  84.  
  85. public Map<String, Object> getAttributes() {
  86. Map<String, Object> attributes = new HashMap<String, Object>();
  87. attributes.put("attribute1", "value1");
  88. attributes.put("attribute2", "value2");
  89.  
  90. return attributes;
  91. }
  92.  
  93. public void parameterActionListener() {
  94.  
  95. FacesContext facesContext = FacesContext.getCurrentInstance();
  96. PhaseId phaseId = facesContext.getCurrentPhaseId();
  97. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  98.  
  99. Map<String, String> requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
  100. String value = requestParameterMap.get("parameter");
  101.  
  102. String phaseName = phaseId.toString();
  103. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  104. " phase of the JSF lifecycle.The parameter value is " + value);
  105. facesContext.addMessage(null, facesMessage);
  106. }
  107.  
  108. public void selectionListener(ActionEvent actionEvent) {
  109.  
  110. UICommand uiCommand = (UICommand) actionEvent.getComponent();
  111. Customer customer = (Customer) uiCommand.getValue();
  112. commandModelBean.setSelectedCustomer(customer);
  113. }
  114.  
  115. public void setCommandModelBean(CommandModelBean commandModelBean) {
  116. this.commandModelBean = commandModelBean;
  117. }
  118. }
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20