How to get context parameter values in JSF application

In this tutorial we are going to show you how to read web application context parameter values in your JSF application, in the bean Java code and straight in the XHTML view as well. Here are our sample application code snippets.

JSF get context param – web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Reading context parameters sample</display-name>

	<!-- Context parameters -->
	<context-param>
		<param-name>myParameter</param-name>
		<param-value>Simple parameter value</param-value>
	</context-param>

	<!-- Welcome page -->
	<welcome-file-list>
		<welcome-file>index.xhtml</welcome-file>
	</welcome-file-list>

	<!-- Staring JSF -->
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- JSF URL mapping -->
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>

</web-app>

We add an context parameter named myParameter with the value of Simple parameter value in the web.xml application descriptor. This value is going to be read in our sample application.

JSF get context param – XHTML view code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
        <title>Reading context params example</title>
    </h:head>
    <h:body>
    	<h:form>
    		<h2>Read the context parameter through bean</h2>
    		<br/>
    		<h:outputText value="#{paramBean.contextParameter}"/>
    		<br/>
    		<h2>Read the context parameter with the EL</h2>
    		<br/>
    		<h:outputText value="#{initParam['myParameter']}"/>
    		<br/>
    	</h:form>
    </h:body>
</html>

In our xhtml sample view we output myParameter context parameter value using the h:outputText tag. At first we read the value from the bean – look a little bit lower for the details on reading context parameter value in the bean code. Next, we display the same value by using EL’s initParam variable. This variable contains, under specific parameter name keys, all the values of context parameters present in the application.

JSF get context param – bean code

package com.itcuties.examples.webapps;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean
public class ParamBean implements Serializable {

	private static final long serialVersionUID = 8541599579318786034L;

	/**
	 * This method returns the myParameter parameter value.
	 * @return
	 */
	public String getContextParameter() {
		return FacesContext
				.getCurrentInstance()
				.getExternalContext()
				.getInitParameter("myParameter");
	}	
}

In the getContextParameter method we read myParameter context parameter value. To do that you need to gain access to the application’s ExternalContext. You can do that easy, by using this method invocation chain – FacesContext.getCurrentInstance().getExternalContext(). When you have the ExternalContext reference, then you can call getInitParameter method on it, which returns the value of the context parameter for the name that was provided to the call.

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>