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

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

5.7 The Article Page

The article page consists of two pieces: the contents of the article and the comment region, which allows users to comment on stories and read others' comments. The first portion is even simpler than the section page, as it need only display the contents of a few properties from the ArticleBean, as shown in Listing 5.8.

Listing 5.8 The article page
<jsp:useBean
  id="article"
  class="com.awl.jspbook.ch05.ArticleBean"/>

<jsp:useBean
  id="user5"
  scope="session"
  class="com.awl.jspbook.ch05.UserInfoBean"/>

<jsp:setProperty name="article" property="articleId"/>
<h2><c:out value="${article.headline}"/></h2>
<i>Posted by <c:out value="${article.author}"/>
at
<fmt:formatDate
  value="${article.time}"
  pattern="MM/dd/yy hh:mm"/>
</i><p>

<c:out value="${article.contents}"/>

Note the use of the fmt:formatDate tag from the previous chapter to format the date.

The comment portion appears below the article contents and shows the list of available comments, along with a form to add an additional one, as shown in Listing 5.9.

Listing 5.9 The comment section
<hr width="80%">
<h2>Comments</h2>

<c:forEach items="${article.comments}" var="comment">
  Posted by <c:out value="${comment.author}"/>
  at <fmt:formatDate
  value="${comment.time}"
  pattern="MM/dd/yy hh:mm"/><br>
  <blockquote>
    <c:out value="${comment.text}"/>
  </blockquote><p>
</c:forEach>

<hr width="80%">

<c:if test="${user5.isLoggedIn}">
  <h2>Comment on this article</h2>
  <form action="comment_result.jsp" method="post">
    <input
      type="hidden"
      name="author"
      value="<c:out value="${user5.name}"/>">
    <input type="hidden"
      name="articleId"
      value="<c:out value="${article.articleId}"/>">

    <textarea name="text" rows="10" cols="30">
    </textarea><br>
    <input type="submit" name="Submit" value="submit">
  </form>
</c:if>

Anyone may read existing comments, so the current set is displayed with a standard c:forEach tag. Java News Today has decided that only logged-in users may add comments. This encourages users to sign up with the site and makes it easier to ban users who abuse the system. Consequently, the input form is wrapped in a c:if tag. The browser view of this page for a user who has logged in is shown in Figure 5.4. Note that this page recognizes a logged-in user in three ways: The login form is gone, the user's name appears in the header, and the comment section is active.

Figure 5.4. The JNT article page.

graphics/05fig04.gif

One new feature to the form itself is that the name of the user is passed in a hidden variable, a common trick for transmitting data from one page to another. Although it would certainly have been possible for the receiving page to set manually the user's name in the CommentBean from the UserInfoBean, providing that information through the form allows the receiving page simply to do one jsp: setProperty instead of having to get properties from multiple places.

The comment result page is much like other pages that have already been considered. The heart of this page will simply set the values from the form into the bean in the standard way:

<jsp:setProperty name="comment" property="*"/>

The CommentBean is designed so that once all the fields have been set, the comment is correctly associated with an ArticleBean. Once again, putting the complex logic in the model has made it very easy to create the view.

    [ directory ] Previous Section Next Section