Use mashups4jsf to generate and consume mashup feed

Source: Internet
Author: User
Tags websphere application server

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

 

newsTableBy expression#{newsList.news}AndNewsListDto binding. Listing 2 showsNewsListDto 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;     }}

 

NewsClass is a very simple bean. It only contains the following attributes:

  1. Title
  2. Description
  3. Link
  4. Category
  5. 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 FeedNewsListData.

 

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 feedNewsListData, 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@FeedComment feed class (NewsListClass), and use@FeedTitle,@FeedDescription,@FeedLinkAnd@FeedItemsSpecify the title, description, link, and feed. Listing 5 showsNewsListClass.


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@ItemFeedComment the feed Item class (NewsClass), and use@ItemTitle,@ItemDescription,@ItemLink,@ItemCategoryAnd@ItemAuthorSpecify the title, description, Link, category, and author of the feed item. Listing 6 showsNewsClass.


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 specifyrssAs 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 showsNewsListFeed.


Figure 2. newslist RSS Feed
 

If you want to export newslist as an atom feedatomThe 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 passrssFeedReaderComponent usage we just usedNewsListThe RSS feed created by the data.

Listing 8 shows how to userssFeedReaderThe 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 (suchtitle,linkAndauthor), 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 showsNewsListFeedrssFeedReaderOutput.


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.