Working with rss4jsf project

In this recipe, you will see how to use a JSF component, rss4jsf, able to show RSS content in JSF pages. As you will see in our example, the newest release of rss4jsf includes the ability to have full control over the HTML generated through facets.

Getting ready

We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library. In addition, we have used rss4jsf, which provides support for JSF 2.0. You can download this distribution from http://code.google.com/p/rss4jsf/ (it is also available through Maven). The rss4jsf library (including necessary dependencies) is in the book bundle code, under the /JSF_libs/rss4jsf JSF 2.0 folder.

How to do it...

For a start we have to specify the tag library, which is http://hexiao.cn/rss4jsf, and the prefix, which is usually rss4jsf or r4j. The rss4jsf/r4j prefixes the simpleRssOutput tag, which supports four important attributes (it also contains an entire set of attributes for applying CSS styles to RSS items):

  • url: The value of this attribute is a string representing the RSS document URL.
  • count: The value of this attribute is an integer representing the number of RSS articles that should be rendered.
  • channelVar: The value of this attribute is a string that maps the RSS channel. We can obtain channel name and number of items through it.
  • itemVar: The value of this attribute is a string that maps an RSS item.
  • entrysSummaryStyleClass, postTimeStyleClass, readMoreStyleClass, rssEntryStyleClass, rssEntryTitleStyleClass, rssSiteNameStyleClass, and rssSiteStyleClass: These attributes indicate CSS classes for styling the CSS result.

The content of the simpleRssOutput tag consists of a set of facets (f:facet), depending on how you decide to format the RSS output. You can see two examples are listed out next:

Example 1

<rss4jsf:simpleRssOutput url=" http://services.devx.com/outgoing/devxfeed.xml"
count="5"
channelVar="channel"
itemVar="item">
<f:facet name="header">
<div class="header">
<h2>#{channel.name}</h2>
<p>#{channel.numberOfItems} items. <
ahref="#{channel.siteUrl}">View Site</a>.</p>
</div>
</f:facet>
<f:facet name="item">
<div class="item">
<h3><a href="#{item.url}">#{item.title}</a></h3>
<p>#{item.author} - #{item.body }</p>
</div>
</f:facet>
</rss4jsf:simpleRssOutput>

Example 2

<rss4jsf:simpleRssOutput
url="http://services.devx.com/outgoing/devxfeed.xml"
count="500">
<f:facet name="item">
<div class="item">
<h:outputLink value="#{item.url}">
<b><h:outputText value="#{item.title}"/></b>
<f:verbatim> - </f:verbatim>
<h:outputText value="#{item.author}"/><br />
<i><h:outputText value="#{item.body}"/></i><br /><br />
</h:outputLink>
</div>
</f:facet>
</rss4jsf:simpleRssOutput>

A possible output may look like the following screenshot:

How to do it...

How it works...

The rss4jsf component takes the RSS feed address and returns the result. We can customize how the result is rendered through facets and CSS style. Notice that we can take control over each piece of the RSS result, such as title, author, content, and so on, which provides us the facility of rendering something really cool!

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: RSS_for_JSF.

You also may want to check the rss4jsf project page at http://code.google.com/p/rss4jsf/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset