JakartaFaces

1 min read

Passing and getting parameters

Using context parameters

web.xml

<context-param>
  <param-name>open.devops.tutorials</param-name>
  <param-value>Open DevOps</param-value>
</context-param>

JSF web page

<h:outputText value="#{initParam['open.devops.tutorials']}"/>
<h:outputText value="#{facesContext.externalContext.initParameterMap['open.devops.tutorials']}"/>

Java code

facesContext.getExternalContext().getInitParameter("open.devops.tutorials");

Passing request parameters with the <f:param> tag

JSF web page

<h:form>
  Click to send name, 'Open' surname, 'DevOps', with f:param:
  <h:commandButton value="Send Open DevOps"  action="#{siteBean.parametersAction()}">
  <f:param id="siteName" name="siteNameParam" value="Open"/>
  <f:param id="siteSurname" name="siteSurnameParam" value="DevOps"/>
  </h:commandButton>
</h:form>

Java code

private String siteName;
private String siteSurname;

...
// getter and setter
...

public String parametersAction() {
  FacesContext fc = FacesContext.getCurrentInstance();
  Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
  siteName = params.get("siteNameParam");
  siteSurname = params.get("siteSurnameParam");
       
  return "some_page";
}

EL 3.0

Name: #{param.siteNameParam} 
Surname: #{param.siteSurnameParam}

JSF code

<h:outputFormat value="Name: {0} Surname: {1}">
  <f:param value="#{siteBean.siteName}" />
  <f:param value="#{siteBean.siteSurname}" /> 
</h:outputFormat>

Output:

Name: Open 
Surname: DevOps
Orestis Pantazos

Orestis Pantazos

DevOps Engineer