In a previous article about Mashups4JSF (see Resources), we learned what Mashups4JSF is, how to configure it, and how to use it for building a useful mashup application. We learned how to use the library with both Apache MyFaces 2.0 and IBM JWL (JavaServer Faces Widget Library) on WebSphere Application Server V7.0.
One of the most common use cases in mashup applications is the ability to export an application's internal data as feeds (RSS or Atom). Before Mashups4JSF, developers had to manually produce the feeds from the application's DTOs (data transfer objects). Now, thanks to Mashups4JSF, exporting application data as feeds is much easier.
In this article, I will explain how to export web application data as RSS feeds using the Mashups4JSF feed producer service, and how to consume it using the feed reader component.
The news application is a JavaServer Faces (JSF) 2.0 application that shows a sample set of news in a data table as shown in Figure 1, and we wish to export this sample set of news as RSS feeds.
Figure 1. The news sample
The code behind the news page is very simple. Listing 1 shows the news dataTable XHTML code in the news.xhtml page.
Listing 1. The news dataTable in the news.xhtml page
<h:dataTable id="newsTable" value="#{newsList.news}" var="varnews"
styleClass="standardTable" headerClass="standardTable_Header"
footerClass="standardTable_Footer"
rowClasses="standardTable_Row1, standardTable_Row2"
columnClasses="standardTable_Column">
<h:column id="titleColumn">
<f:facet name="header">
<h:outputText value="#{messages.application_label_title}"
id="titleText">
</h:outputText>
</f:facet>
<h:outputLink value="#{varnews.link}">
<h:outputText id="title" value="#{varnews.title}">
</h:outputText>
</h:outputLink>
</h:column>
<h:column id="authorColumn">
<f:facet name="header">
<h:outputText value="#{messages.application_label_author}"
id="authorText">
</h:outputText>
</f:facet>
<h:outputText id="author" value="#{varnews.author}">
</h:outputText>
</h:column>
<h:column id="categoryColumn">
<f:facet name="header">
<h:outputText value="#{messages.application_label_category}"
id="categoryText">
</h:outputText>
</f:facet>
<h:outputText id="category" value="#{varnews.category}">
</h:outputText>
</h:column>
<h:column id="descColumn">
<f:facet name="header">
<h:outputText value="#{messages.application_label_description}"
id="descText">
</h:outputText>
</f:facet>
<h:outputText id="desc" value="#{varnews.desc}">
</h:outputText>
</h:column>
</h:dataTable>
|
The newsTable is bound with the
NewsList DTO through the expression
#{newsList.news}. Listing
2 shows the code of the NewsList DTO.
It has a static list of news objects. It can hold real data from a
database, but for the purpose of simplicity, a static list is used.
Listing 2. The NewsList DTO
@ManagedBean
public class NewsList {
static List <News> news = new ArrayList<News>();
// Sample news list
static {
for (int i = 0; i < 10; ++i) {
news.add(new News(
"Title" + i,
"Description" + i,
"http://www.google.com/search?q=" + i,
"Category" + i,
"Author" + i));
}
}
public List<News> getNews() {
return news;
}
}
|
The News class is a very simple bean. It
contains only the following attributes:
- Title
- Description
- Link
- Category
- Author
See Listing 3.
Listing 3. The News DTO
public class News {
private String title;
private String desc;
private String link;
private String category;
private String author;
...
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
|
Now let's see how to export the NewsList data as
an RSS feed.
Export the application data as an RSS feed
Before going into the details of exporting the news list as an RSS feed, you may need to remember how to configure the Mashups4JSF library with the JSF 2.0 applications as described in the previous Mashups4JSF article (see Resources). When using the Mashups4JSF feed servlet, be sure to download the latest stable snapshot of Mashups4JSF 0.0.3(see Resources).
For exporting the NewsList data as an RSS feed
using Mashups4JSF, we will do the following steps in our JSF
application.
Register the Mashups4JSF feed servlet
Register the MashupFeedServlet of Mashups4JSF in
your application's web.xml as shown in Listing
4.
Listing 4. Registering the Mashups4JSF feed servlet
<servlet> <servlet-name>mashupFeedServlet</servlet-name> <servlet-class>com.googlecode.mashups.servlets.MashupFeedServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mashupFeedServlet</servlet-name> <url-pattern>/mashupFeedServlet</url-pattern> </servlet-mapping> |
Annotate the feed class (NewsList class) with
the @Feed and specify the title, description,
link and items of the feed using the
@FeedTitle,
@FeedDescription,
@FeedLink, and
@FeedItems. Listing 5
shows the NewsList class after applying the
annotations.
Listing 5. The NewsList class after applying the annotations
@ManagedBean
@Feed
public class NewsList {
static List <News> news = new ArrayList<News>();
// Sample news list
static {
for (int i = 0; i < 10; ++i) {
news.add(new News(
"Title" + i,
"Description" + i,
"http://www.google.com/search?q=" + i,
"Category" + i,
"Author" + i));
}
}
@FeedItems
public List<News> getNews() {
return news;
}
@FeedTitle
public String getTitle() {
return TITLE;
}
@FeedDescription
public String getDescription() {
return DESCRIPTION;
}
@FeedLink
public String getLink() {
return LINK;
}
private final String TITLE = "News List";
private final String DESCRIPTION = "News List Description";
private final String LINK = "http://somenewschannel.com";
}
|
Annotate the feed item class (News class) with
the @ItemFeed and specify the title,
description, link, category and author of the feed item using the
@ItemTitle,
@ItemDescription,
@ItemLink,
@ItemCategory, and
@ItemAuthor. Listing 6
shows the News class after applying the
annotations.
Listing 6. The News class after applying the annotations
@FeedItem
public class News {
private String title;
private String desc;
private String link;
private String category;
private String author;
...
@ItemTitle
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ItemDescription
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@ItemLink
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@ItemCategory
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@ItemAuthor
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
|
After we have annotated both the feed and feed item classes, we are almost
done. All what we need to do is to render the feed by passing the feed
class as a parameter to the feed servlet and specify
rss as an output as shown in Listing 7.
Listing 7. Rendering the RSS feed by passing the feed class as a parameter to the feed servlet
#{request.contextPath}/mashupFeedServlet?feedClass=dto.NewsList&output=rss
|
Figure 2. shows the NewsList feed after
exporting as an RSS feed.
Figure 2. The NewsList RSS Feed
If you want to export the NewsList as ATOM feed, you can do this by
specifying atom as an output of the feed
servlet.
Consume the RSS feed using the feed reader
Adding to the RSS and ATOM feed exports, Mashups4JSF also allows consuming
any RSS, ATOM or JSON feeds inside the JSF application. So let's see how
to consume the RSS feed we just produced for the
NewsList data, using the
rssFeedReader component.
Listing 8 shows how to consume the RSS feed using
the rssFeedReader component in the
consumer.xhtml page.
Listing 8. The rssFeedReader component in the consumer.xhtml page
<mashup:rssFeedReader
feedURL="http://#{facesContext.externalContext.request.remoteHost}:
#{facesContext.externalContext.request.localPort}
#{facesContext.externalContext.request.contextPath}
/mashupFeedServlet?feedClass=dto.NewsList&output=rss"
maximumCount="5"
channelVar="channel"
itemVar="item"
itemIndex="itemIndex">
<f:facet name="channel">
<h4>#{messages.application_channel_desc}#{channel.description}</h4>
</f:facet>
<f:facet name="item">
<b>#{messages.application_item_index} </b>#{itemIndex}<br />
<b>#{messages.application_item_title} </b>#{item.title}<br />
<b>#{messages.application_item_link} </b>
<h:outputLink value="#{item.link}">
#{item.link}
</h:outputLink><br />
<b>#{messages.application_item_author} </b>#{item.author}<br />
<b>#{messages.application_item_category} </b>
#{item.categories[0].name}<br /><br />
</f:facet>
</mashup:rssFeedReader>
|
As shown in Listing 8, we specified:
- the RSS feed URL,
- the channel variable used to access the RSS channel information
(
descriptionfor example), - the item variable which will access every RSS feed item
(
title,linkandauthorfor example), and - the item index.
In the channel facet, we can specify which attributes we want to display for the RSS channel and in the item facet; we can specify which attributes we want to display for every feed item.
Figure 3 shows the output of the
rssFeedReader for the
NewsList RSS feed.
Figure 3. The rssFeedReader output for the NewsList RSS feed
Adding to the different mashup components and services, Mashups4JSF abstracted the ways of mashup feed exporting and importing for JSF applications. Before Mashups4JSF, developers had to manually produce feeds from an application's DTOs (data transfer objects). Now, thanks to Mashups4JSF, exporting application data as feeds is easier. In this article, we learned how to represent the JSF application data as RSS feeds using the Mashups4JSF Feed Servlet. We also learned how to consume the produced feed using the rssFeedReader component.
Learn
- An introduction to Mashups4JSF: Mashups made easy (Hazem Saleh,
developerWorks, July 2010): Learn about the architecture of Mashups4JSF
and configuration of the library. Create a mashup application with few
lines of code, using Mashups4JSF and the IBM JWL (JSF Widget Library) on
the WebSphere Application Server 7.0.
-
Apache MyFaces Project home page:
Find more about this project of the Apache Software Foundation.
-
Mashups4JSF home page:
Access the library that allows constructing mashups in the Java™
world and JSF applications easily.
-
The Mashups and JavaServer™
Faces Integration Library: Check out the Mashups4JSF
demos.
- Events of interest: Check out upcoming conferences, trade shows,
and webcasts that are of interest to IBM open source
developers.
- developerWorks
Open source zone: Find extensive how-to information, tools, and
project updates to help you develop with open source technologies and use
them with IBM's products.
- developerWorks on
Twitter: Follow us.
Get products and technologies
- Latest stable snapshot of Mashups4JSF
0.0.3
- IBM trial
software: Innovate your next open source development project using
trial software, available for download or on DVD.
Discuss
- developerWorks
community: Connect with other developerWorks users while exploring
the developer-driven blogs, forums, groups, and wikis. Help build the Real world open source group in the developerWorks
community.

Hazem Saleh has six years of experience in JEE and open source technologies. He is an Apache MyFaces committer and the initiator of many components in the MyFaces projects, such as Tomahawk CAPTCHA, Commons ExportActionListener, Media, PasswordStrength, and others. He is the founder of GMaps4JSF (an integration project that integrates Google Maps with the JavaServer Faces) and Mashups4JSF (an integration project that integrates mashup services with the JavaServer Faces). He is the author of the "The Definitive Guide to Apache MyFaces and Facelets (Apress)" book and many JSF articles, is a developerworks contributing author, and a JSF public speaker. He is now working for IBM Egypt as a staff engineer. Hazem is recognized as a Subject Matter Expert in web 2.0 technologies.



