• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rendering a List in a JSF page

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using a ContextListener to build a list containing domain objects during the application's context initialization.
Now I want to display this list (which represents DB table rows) in the first page that renders.
I have assigned this list to the Servlet Context and I have referenced this context in my page like this:
FacesContext fc = FacesContext.getCurrentInstance();
ServletContext sc = (ServletContext) fc.getExternalContext().getContext();
List patients = sc.getAttribute("PATIENT_LIST");
The PATIENT_LIST attribute is set in the context listener.

First, is this a sensible way to initialize such a list in a JSF environment?
Second, because I have not used a backing bean to hold the list, how do I use the JSF tags such as <h:dataTable...> to render the contents of this list?
(Using JSTL for this is easy but I need to use JSF tags.)
Afterthought, perhaps I should implement a backing bean and assign the list to that bean during initialization and dispense with the Servlet Context approach.
TIA...
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Make use of an application scoped managed bean.
2) Check this article for examples: http://balusc.blogspot.com/2006/06/using-datatables.html
3) Indeed, you should.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF Rule #1: The more javax.faces code in the bean, the more likely you're doing it wrong.

If you have an application scope bean and you want to display it in a datatable, the simplest way to do that is to construct a backing bean and inject the application-scope bean into it using the managed-property element of the faces-config.xml file. No javax.faces code is required - just setter/getter methods in the backing bean.

You can't present the applicaition-scope object directly to the view no matter which method you use to gain access to it, however - the dataTable controller requires additional model information to support its ability to iterate and to reference rows within the dataTable view. That means you need a javax.faces.Datamodel object to serve as the fronting object. The DataModel and its sibling the SelectItem are the only 2 javax.faces classes many JSF apps really need. There is a time and a place for more involved mechanisms, but most people overuse them and the result is lower productivity and code which has a lower level of reusability and maintainability.

You mentioned being unsure of how to initialize the app-scope object. You can initialize it as a JSF-scope managed bean, you can construct it in a start-time servlet method, you can use Spring. It's mostly a matter of what works best for you.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic