Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Use Java) 30.2 (Guestbook Application) Create a JSF web app that allows users t

ID: 668483 • Letter: #

Question

(Use Java)

30.2 (Guestbook Application) Create a JSF web app that allows users to sign and view a guestbook.
Use the Guestbook database to store guestbook entries. [Note: A SQL script to create the
Guestbook database is provided in the examples directory for this chapter.] The Guestbook database
has a single table, Messages, which has four columns: Date, Name, Email and Message. The database
already contains a few sample entries. Using the AddressBook app in Section 30.2 as your guide, create
two Facelets pages and a managed bean. The index.xhtml page should show the Guestbook entries
in tabular format and should provide a button to add an entry to the Guestbook. When the
user clicks this button, display an addentry.xhtml page. Provide h:inputText elements for the user’s
name and email address, an h:inputTextarea for the message and a Sign Guestbook button to submit
the form. When the form is submitted, you should store in the Guestbook database a new entry
containing the user’s input and the date of the entry.

Explanation / Answer

<html>
  

<head><title>Hello</title></head>
  

<%@ taglib uri="#" prefix="h" %>
  

<%@ taglib uri="#" prefix="f" %>
  

<body bgcolor="white">
  

<f:view>
  

<h:form id="helloForm" >
  

<h2>Hi
  

<h:outputText value="#{UserNumberBean.minimum}"/> to
  

<h:outputText value="#{UserNumberBean.maximum}"/>.
  

Can you guess it?</h2>
  

<h:graphicImage id="waveImg" url="/wave.med.gif" />
  

<h:inputText id="userNo" value="#{UserNumberBean.userNumber}"
  

validator="#{UserNumberBean.validate}"/>
  

<h:commandButton id="submit" action="success"
  

value="Submit" />
  

<p>
  

<h:message
  

id="errors1" for="userNo"/>
  

</h:form>
  

</f:view>
  

</body>
  

</html>
  

package com.example.guestbook;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

/**
* OfyHelper, a ServletContextListener, is setup in web.xml to run before a JSP is run. This is
* required to let JSP's access Ofy.
**/
public class OfyHelper implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// This will be invoked as part of a warmup request, or the first user request if no warmup
// request.
ObjectifyService.register(Guestbook.class);
ObjectifyService.register(Greeting.class);
}

public void contextDestroyed(ServletContextEvent event) {
// App Engine does not currently invoke this method.
}
}
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.example.guestbook.OfyHelper</listener-class>
</listener>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main" />
<title>Create Feedback</title>   
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLinkTo(dir:'')}">Home</a></span>
<span class="menuButton"><g:link class="list" action="list">Feedback List</g:link></span>
</div>
<div class="body">
<h1>Create Feedback</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
div class=“date”>${flash.date}</div>
  
div class=“name”>${flash.email}</div>
</g:if>

<g:hasErrors bean="${feedback}">
<div class="errors">
<g:renderErrors bean="${feedback}" as="list" />
</div>
</g:hasErrors>
</div>
</g:hasErrors>
<g:form action="save" method="post" >
<div class="dialog">
<table>
<tbody>
  
<tr class="prop">
<td valign="top" class="name">
<label for="name">Name:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:feedback,field:'name','errors')}">
<input type="text" id="name" name="name" value="${fieldValue(bean:feedback,field:'name')}"/>
</td>
</tr>
  
<tr class="prop">
<td valign="top" class="name">
<label for="feedback">Feedback:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:feedback,field:'feedback','errors')}">
<input type="text" id="feedback" name="feedback" value”>
${fieldValue(bean:feedback,field:'feedback')}"/>
</td>
</tr>
  
  
  
</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><input class="save" type="submit" value="Create" /></span>
</div>
</g:form>
</div>
</body>
</html>