Accessing Context Data in Beans using JSF
By: Henry
Although JSF makes it easy for you to connect the view with the model without writing any code, at times you may need direct access to the request data or other data of your web application.JSF provides access to the request data and other data through the FacesContext object. As the Javadoc states, “FacesContext contains all of the per-request state information related to the processing of a single JavaServer Faces request, and the rendering of the corresponding response . . . . A FacesContext instance is associated with a particular request at the beginning of request processing.†Because a FacesContext instance, and the objects it contains, are associated with the thread processing a particular request, any references to the FacesContext instance or its contained objects must not be passed to another thread or stored for subsequent requests.
You can access the FacesContext object like this:
FacesContext context = FacesContext.getCurrentInstance();
After you have a reference to the FacesContext for the request, you can access all the request and application data through the ExternalContext object. The reference to the ExternalContext object is obtained through the call to the getExternalContext() method. Table below shows the methods of ExternalContext that return collections of request and application data. You can use these methods to access request data, session data, request header data, cookie data, and other sets of data in the application.
Most likely, if you are accessing request or application data in
a managed bean, you will be accessing request data or session data. As you can see in
Table, you access request data through the getRequestParameterMap() or
getRequestParameterValuesMap() method. You can access session data through the getSessionMap() method.
Archived Comments
1. Is so easy work with FacesContext. For me, the getSessionMap method is an big guy that help me work
View Tutorial By: Fred at 2009-03-11 08:42:59
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
<convertNumber> and <convertDateTime> in JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
How to open a new browser window from my JSF page?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Controlling Page Navigation in JSF - Static and Dynamic Navigation