站内搜索: 请输入搜索关键词
当前页面: 图书首页 > JavaServer Pages, Second Edition

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

8.6 Java News Today and XML

In order to use these new features, Java News Today will be creating XML versions of a few of its pages, notably a stripped-down version of the index and section pages. These pages will not contain some of the dynamic elements, such as the login form or quiz. Both of these elements can be accomplished with a single JSP, as the only difference is whether to obtain the article list from the section or the edition. This JSP is shown in Listing 8.10.

Listing 8.10 The XML version of JNT
<%@ page contentType="text/xml" %>
<%@ taglib prefix="c"
    uri="http://java.sun.com/jstl/core" %>


<jsp:useBean
  id="currentSection"
  class="com.awl.jspbook.ch07.SectionBean"/>

<jsp:useBean
  id="edition8"
  scope="request"
  class="com.awl.jspbook.ch07.EditionBean"/>


<jsp:setProperty
  name="currentSection"
  property="sectionId"/>


<c:choose>
  <c:when test="${empty currentSection.sectionId}">
    <c:set var="articles"
           value="${edition8.recentArticles}"/>
    <c:set var="sectionName" value="Java News Today"/>
  </c:when>
  <c:otherwise>
    <c:set var="articles"
          value="${currentSection.articles}"/>
    <c:set var="sectionName"
           value="${currentSection.name}"/>
  </c:otherwise>
</c:choose>


<javaNewsToday>
  <section><c:out value="${sectionName}"/></section>
  <articles>
    <c:forEach items="${articles}" var="article">
      <article>
        <headline>
          <c:out value="${article.headline}"/>
        </headline>
        <summary>
          <c:out value="${article.summary}"/>
        </summary>
         <date>
          <c:out value="${article.createdDate}"/>
        </date>
      </article>
    </c:forEach>
  </articles>
</javaNewsToday>

The block of code at the top of this example determines whether the user is requesting a section or the index page, based on whether a sectionId has been provided. The code block then stores a list of articles in a variable called articles and a section name in sectionName. This technique has not been seen before, but it works very much like the variables created by the c:import tag. The rest of the page is a standard c:forEach used to create the XML.

The goal of this XML representation of the site is not to replace the existing pages, which are working well enough as they are. Instead, this XML layer allows Java News Today to offer its content to other sites, as well as directly to users.

As mentioned, the c:import tag can pull pages from anywhere, not just locally. This means that if another Java site梥ay, javamonkeys.com梬ere interested in giving its users access to Java News Today's articles, it could import the XML file and then format it with its own XSLT file in order to make it mesh seamlessly with the rest of its site.

This ability to provide one site's content to other sites is called syndication and is quite popular. When properly done, it can benefit both sites. In this case, javamonkeys.com can offer users additional reasons to visit its site; in exchange, these additional users will learn about Java News Today and may wish to visit the JNT site directly. It is also possible for sites to charge each other for syndicated content or to swap advertising banners.

What makes all this possible is that XML is a standard format. Javamonkeys.com doesn't need to know anything about JNT's database layout, and JNT doesn't need to allow javamonkeys.com to access its database directly, which could be a security risk.

    [ directory ] Previous Section Next Section