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

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

7.2 Adding Articles

With the new database and coding conventions established, adding new articles to the database is quite simple. First, a link to the article creation page must be added to the left-hand navigation, and a check must be done to ensure that this link is available only to reporters. This is handled by a simple addition to the navigation, shown in Listing 7.2.

Listing 7.2 New link for reporters
<c:if test="${user7.isReporter}">
  <div class="bordered">
    <a href="create_article.jsp">Create a new article</a>
  </div>
</c:if>

If the user has not yet logged in, the isReporter property will be empty, that is, neither true nor false. In this case, the test will still fail, and the link will not be shown. The check here is technically not sufficient, as it does nothing to prevent a malicious user from going to create_article.jsp directly by entering the URL in his or her browser. To prevent this, a controller is needed to handle site security; one will be built in Chapter 12.

The page to create articles is another standard HTML form. The author will need to be able to select the section to which the new story should be added, a way to mark which keywords are relevant, and a big text box for the contents. The JSP is shown in Listing 7.3, and the result as viewed in a browser appears in Figure 7.1.

Figure 7.1. The article creation page.

graphics/07fig01.gif

Listing 7.3 The article creation page
<%@ taglib prefix="c"
    uri="http://java.sun.com/jstl/core" %>

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

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

<jsp:include page="top.jsp" flush="true">
  <jsp:param name="title" value="Create Article"/>
</jsp:include>

<table class="form">
<form action="article_handler.jsp" method="post">
<input
   type="hidden"
   name="authorId"
   value="<c:out value="${user7.userInfoId}"/>">

<tr>
  <td class="label">Section:</td>
  <td>
    <select name="sectionId">
      <c:forEach items="${edition7.allSections}"
                       var="section">
      <option value="<c:out
              value="${section.sectionId}"/>">
      <c:out value="${section.name}"/>
      </c:forEach>
    </select>
  </td>
</tr>

<tr>
  <td class="label">Keywords:</td>
  <td>
    <c:forEach items="${edition7.allKeywords}"
                     var="keyword">
      <input
        type="checkbox"
        name="keywordId"
        value=<c:out value="${keyword.keywordId}"/>>
       <c:out value="${keyword.name}"/><br>
     </c:forEach>
  </td>
</tr>

<tr>
  <td class="label">Headline:</td>
  <td><input type="text" name="headline"></td>
</tr>

<tr>
  <td class="label">Summary:</td>
  <td><input type="text" name="summary"></td>
</tr>

<tr>
  <td class="label">Text:</td>
  <td>
    <textarea name="text" rows="5" cols="30">
    </textarea>
  </td>
</tr>

<tr>
  <td colspan="2" align="right">
    <input type="submit" name="go" value="Set Preferences">
  </td>
</tr>
</form>
</table>

<jsp:include page="bottom.jsp" flush="true"/>

Note that the lists of available sections and keywords come from the EditionBean. This is appropriate, as this bean acts as the master container for all options on the site, as well as the specific set of those options selected by each user.

Because all the work is done in a bean representing the model, as it should be, the view portion of the article handler is almost trivial:

<jsp:setProperty bean="article" property="*">
<jsp:setProperty
  name="article"
  property="useNowAsDate"
  value="true"/>
<jsp:setProperty bean="article" property="save"
                 value="true">

The first line sets all the properties of the article, including the section and the text. The first line also sets an array of keyword IDs, which the bean will use internally to set up the proper entries in the article_keyword table. The second line sets a special property, called useNowAsDate, of the bean. When this property is set, it will set the underlying createdDate property to the current time. It would be possible to set createdDate directly from the page, but working with date properties can be cumbersome, so the useNowAsDate property was provided as a convenience.

Finally, the third line will then perform the save and will write all the data to the database. It might seem that this extra jsp:setProperty tag could be avoided by using a hidden input field, as was done in Listing 5.9, to pass the user's name to the comment handler: perhaps something like

<input type="hidden" name="save" value="true">

However, this is not guaranteed to work. Nothing in the JSP specification says anything about the order in which properties will be set. If the save property were sent along with text and sectionId, it is quite possible that first sectionId would be set, then save, and finally text. The net effect would be that an article would be placed in the database with a sectionId but no content!

    [ directory ] Previous Section Next Section