Creating Poll Templates

The following very simple examples are based on the templates in the poll-demo publication.

Here is the main content item template, called art_default.jsp. All it does is check the type (articleTypeName) of the current content item, and if it is poll, transfers control to render-poll.jsp. In a real publication, of course, this template would also check for other content types and transfer control to other templates as well.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
  <title>${publication.name}</title>
 </head>

 <body>
  <h1>${article.title}</h1>
  <c:if test="${article.articleTypeName == 'poll'}">
   <jsp:include page="render-poll.jsp"/>  
  </c:if>
 </body>
</html>

Here is the content of render-poll.jsp, which actually displays the polls:

<%@ taglib prefix="poll" uri="http://www.escenic.com/taglib/escenic-poll" %>
<%@ taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>

<poll:use id="poll">
  <span>${article.fields.question}</span>
  <c:choose>
    <c:when test="${mode eq 'vote'}">
      <html:xhtml />
      <html:form action="/poll/vote">
        <ul>
          <html:hidden property="mentometerId" value="${article.id}"/>
          <html:hidden property="publicationId" value="${publication.id}"/>
          <html:hidden property="redirectTo" value="${article.url}"/>
          <c:forEach items="${poll.mentometerOption}" var="option">
            <li>
              <html:radio property="vote" value="${option.articleElement}"/>
                ${option.title} - ${option.votes}
            </li>
          </c:forEach>
          <html:submit/>
        </ul>
      </html:form>
    </c:when>
    <c:when test="${mode eq 'voted'}">      
      <ul>
        <c:forEach items="${poll.mentometerOption}" var="option">
          <li>${option.title} - ${option.votes}</li>
        </c:forEach>
      </ul>      
    </c:when>
  </c:choose>
</poll:use>

The most significant points in the above listing are highlighted and described below:

article.fields.question

This simply displays the question field of the poll content item.

poll:use

This tag is provided with the Poll plug-in, in the escenic-poll tag library. For detailed information, see Tag Library Reference. It creates a Mentometer bean (see Mentometer) based on the current content item and makes it the current bean. The Mentometer bean is also provided with the Poll plug-in, and represents a poll. It contains an array of MentometerOption beans (see MentometerOption), one for each alternative (svar) field in the content item. The name of the Mentometer bean is set with the poll:use tag's id attribute, so in this case the bean is called poll.

mode eq 'vote'

The request attribute mode can be used to prevent poll visitors voting more than once: it may be set either to vote (the visitor is allowed to vote) or voted (the visitor has already voted and may not vote again). This template allows visitors who are not allowed to vote to see the current results. The mode attribute will only ever be set to voted if you configure the Poll plug-in to check for multiple voting. To do this you must set the checkCookieEnabled property in MentometerManager.properties to true (it is set to false by default). For instructions on how to do this, see Set MentometerManager Properties.

html:form

This is a Struts tag used to handle the display of action forms. The fields in the form are automatically linked to current bean (in this case the Mentometer bean created with the poll:use tag). The fields in the form can then be linked to the bean's properties by means of the Struts tags' property attributes (see memtometerId below). The action to be executed when the form is submitted is set to /poll/vote - the Struts action defined in struts-config.xml (see Editing struts-config.xml for details).

mentometerId, publicationId, redirectTo

These action form properties are set by defining hidden form fields and using the html:hidden tag's property attribute to associate them with the correct properties. The mentometerId and publicationId properties must be set to the current content item and publication IDs. redirectTo determines what page is displayed after the visitor has submitted a vote. In this case it is set to the URL of the current content item (meaning the same page is redisplayed).

poll.mentometerOption

The Mentometer bean's mentometerOption property is a list of MentometerOption beans, one for each alternative answer. The forEach element here cycles through all of these beans, assigning each one in turn to a variable called option.

html:radio

This Struts tag displays a radio button and associates it with the action form's vote property, so that when the user selects a radio button and submits the form, a vote is cast for the selected alternative.

mode eq 'voted'

If the user is not allowed to vote, then the alternatives and current votes are displayed, but no radio buttons or submit button.

This template will produce output that looks like this for users who are allowed to vote:

graphics/poll-vote.png

or like this, for users who have already voted:

graphics/poll-voted.png