In an earlier article about mashups4jsf (see references), we learned what mashups4jsf is, how to configure it, and how to use it to build a useful mashup application. We also learned how to use libraries on WebSphere Application Server v7.0 in combination with Apache MyFaces 2.0 and ibm jwl (JavaServer faces widget library ).
One of the most common use cases in a mashup application is to export internal data of the application in the form of feed (RSS or atom. Before the emergence of mashups4jsf, developers need to manually generate a feed from the application DTO (data transmission object. Thanks to mashups4jsf, it makes it easier to export application data in the form of feed.
In this article, I will explain how to use the mashups4jsf feed Generator Service (producer service) to export web application data in the form of RSS feed and how to use the data through the feed reader component.
Browse news applications
A news application is a JavaServer faces (JSF) 2.0 application that displays a sample news set in a data table, as shown in figure 1, we want to export this sample news set as an RSS feed.
Figure 1. News sample
The Code on the news page is very simple. Listing 1 shows the XHTML code of the news able on the news.xhtml page.
Listing 1. News able on the news.xhtml page
newsTable
By expression#{newsList.news}
AndNewsList
Dto binding. Listing 2 showsNewsList
Dto code. It contains a static list of news objects. It can save real data from the database, but for convenience, a static list is used here.
List 2. newslist DTO
@ManagedBeanpublic 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; }} |
News
Class is a very simple bean. It only contains the following attributes:
- Title
- Description
- Link
- Category
- Author
See listing 3.
Listing 3. 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 take a look at how to export in the form of RSS FeedNewsList
Data.
Back to Top
Export application data in the form of RSS Feed
Before exporting a news list in the form of RSS feed, you may need to know how to configure the mashups4jsf library for the JSP 2.0 application, a previous mashups4jsf article (see references) this section describes the content. When using mashups4jsf feed servlet, make sure that the latest stable snapshot of mashups4jsf 0.0.3 is downloaded (see references ).
Use mashups4jsf to export data as an RSS feedNewsList
Data, we need to perform the following steps in the JSF application.
Register mashups4jsf feed Servlet
Register mashups4jsf'sMashupFeedServlet
, As shown in Listing 4.
Listing 4. Register 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> |
Comment feed class
Use@Feed
Comment feed class (NewsList
Class), and use@FeedTitle
,@FeedDescription
,@FeedLink
And@FeedItems
Specify the title, description, link, and feed. Listing 5 showsNewsList
Class.
Listing 5. newslist class after application Annotation
@ManagedBean@Feedpublic 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"; } |
Use@ItemFeed
Comment the feed Item class (News
Class), and use@ItemTitle
,@ItemDescription
,@ItemLink
,@ItemCategory
And@ItemAuthor
Specify the title, description, Link, category, and author of the feed item. Listing 6 showsNews
Class.
Listing 6. News class after application Annotation
@FeedItempublic 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; }} |
Present feed
After commenting on the feed and feed categories, you have basically completed all the work. The next step is to render the feed. The implementation method is to parse the feed as the feed servlet parameter and specifyrss
As the output, as shown in listing 7.
Listing 7. parse the feed class as the feed servlet parameter to present the RSS Feed
#{request.contextPath}/mashupFeedServlet?feedClass=dto.NewsList&output=rss |
Figure 2 showsNewsList
Feed.
Figure 2. newslist RSS Feed
If you want to export newslist as an atom feedatom
The output of the feed servlet.
Back to Top
Use RSS feed via feed reader
In addition to RSS and atom Feed Export, mashups4jsf allows any RSS, Atom, or JSON feed within the JSF application. Therefore, let's see how to passrssFeedReader
Component usage we just usedNewsList
The RSS feed created by the data.
Listing 8 shows how to userssFeedReader
The component uses RSS feed.
Listing 8. rssfeedreader component on 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"> |
As shown in listing 8, the following content is specified:
- RSS feed URL,
- Channel variables used to access RSS channel information (for example,
description
),
- Item variable, which will access all RSS feed items (such
title
,link
Andauthor
), And
- Index.
In the channel section, we can specify the attribute to be displayed for the RSS feed, while in the item section, we can specify the attribute to be displayed for each feed item.
Figure 3 showsNewsList
FeedrssFeedReader
Output.
Figure 3. rssfeedreader output of newslist RSS Feed
Conclusion
In addition to various mashup components and services, mashups4jsf abstracts the methods for exporting and importing mashup feeds to JSF applications. Before the emergence of mashups4jsf, developers need to manually generate a feed from the application DTO (data transmission object. Thanks to mashups4jsf, it makes it easier to export application data in the form of feed. In this article, we learned how to use mashups4jsf feed servlet to represent JSF application data in the form of RSS feed. We also learned how to use these feeds through the rssfeedreader component.
References
Learning
- Introduction to mashups4jsf: Make mashups simpler (Hazem Saleh, developerworks, July 2010): Learn about the mashups4jsf architecture and library configuration. Use mashups4jsf and ibm jwl (JSF widget library) on WebSphere Application Server 7.0 to create a mashup application with a small amount of code.
- Apache MyFaces project homepage: learn more about the Apache Software Foundation project.
- Mashups4jsf homepage: access the mashups4jsf library, which allows you to easily build mashups in Java and JSF applications.
- Mashups and JavaServer Faces integration library: Watch the mashups4jsf demonstration.
- Activities of interest: Learn about upcoming conferences, exhibitions, and network broadcasts for IBM open source developers.
- Stay tuned to developerworks technical events and network broadcasts.
- Visit the developerworks open source area to get a wealth of how-to information, tools and project updates, and the most popular articles and tutorials to help you develop with open source technology, they are used in combination with IBM products.