HowTo: A guestbook for demo2
Introduction
This Use Case will show how to implement a guestbook for demo2.
Used files
new:
INSTALLATIONSDIR\webroot\demo2\Guestbook\guestbook.jsp INSTALLATIONSDIR\xsl\demo2\Guestbook\guestbook.xsl
changed: INSTALLATIONSDIR\webroot\demo2\includes\top.ijsp
Building guestbook.jsp
First of all we create guestbook.jsp in INSTALLATIONSDIR\webroot\demo2\Guestbook\.
<%@include file="../../includes/header.ijsp"%> <% relpath = "../../"; %> <%@include file="../../includes/top.ijsp"%> <h1>DAS GäSTEBUCH</h1> <%@include file="../../includes/bottom.ijsp"%>
The @include... lines include other JSP-files, which do things like menu etc.. If you read the HowTo Implement a login: demo2? you will know one of them (top.ijsp) already.
The page should look like this:
Implementing the page in the menu
We want to have the guestbook in the side-navigation:
top.ijsp line 54
<td class="greybox"> <!-- content --> <a href=<%=relpath + "modules/Forum/getMainEntries.jsp"%>>Forum</a> <br> <a href=<%=relpath + "modules/Guestbook/guestbook.jsp"%>>Gästebuch</a> <!-- content --> </td>
The module guestbook
First we have to implement the guestbook module:
guestbook.jsp line 6:
<h1>DAS GäSTEBUCH</h1> <% Guestbook gb = new Guestbook(core); %> <%@include file="../../includes/bottom.ijsp"%>
From now on we can use the variable gb - to access the modul. Display of the guestbook:
To display the guestbook we have to call the getEntry method:
guestbook.jsp line 7:
<%
Guestbook gb = new Guestbook(core);
gb.guestbookView.moduleName = "global";
out.print(core.parseDom(gb.getEntry(), "show.xsl"));
%>
After the reload of the page - the XML-tree of the guestbook will be displayed. This is because of several things:
- out.print - prints out something on the website
- core.parseDom - parses a xml-tree with a xsl
- gb.getEntry() - returns the xml-tree of the guestbook
- show.xsl - this is a "debug"-xsl which shows the XML-tree as HTML (i.g. prints out the XML-tree) - this is useful to see what a function returns (see all documentation ;-))
We parse the return of gb.getEntry (return = XML-Tree) with the xsl-file "show.xsl" - this just prints out the return-xml-tree of gb.getEntry. It is very useful for creating a specific xsl-file. The data of the xml-tree should now be parsed through an XSL-file which displays the data correctly in the form of a guestbook.
Creation of guestbook.xsl
Create the file guestbook.xsl:
guestbook.xsl line 1:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> Hier ist das Guestbook! </xsl:template> </xsl:stylesheet>
Now we wanna use the .xsl - therefore we have to make a little change in guestbook.jsp:
guestbook.jsp line 7:
<%
Guestbook gb = new Guestbook(core);
gb.guestbookView.moduleName = "global";
out.print(core.parseDom(gb.getEntry(), "demo2/Guestbook/guestbook.xsl"));
out.print(core.parseDom(gb.getEntry(), "show.xsl"));
%>
"Here is the guestbook" will be shown now above the XML-Tree. Now we can concentrate on the XSL-part of the guestbook und show the data correctly.
Display the guestbook-entries
For the beginning we just want to display all entries of the guestbook one below the other:
guestbook.xsl line 3:
<xsl:template match="/"> <xsl:for-each select="GUESTBOOK/ENTRY"> <p> <xsl:value-of select="MESSAGE"/> </p> </xsl:for-each> </xsl:template>
Now we can already see all entries - but still unformatted.
Der final cut
Next we want to make the guestbook nicer and better integrated into the design. Furthermore we want to show the username next to each entry.
guestbook.xsl line 3:
<xsl:template match="/"> <xsl:for-each select="GUESTBOOK/ENTRY"> <h1> <xsl:value-of select="CREATE/USER/NAME"/> </h1> <div class="whiteBox"> <div class="whiteBoxContent"> <xsl:value-of select="MESSAGE"/> </div> </div> <div class="smallRight"> am <xsl:value-of select="CREATE/DATE"/> </div> <br clear="all" /> </xsl:for-each> </xsl:template>
In addition it should be possible to post with format information. To do this we have to show them first:
The guestbook module filters all HTML-formattings automatically, so that no bad code could be displayed.
guestbook.jsp line 7:
<%
Guestbook gb = new Guestbook(core);
out.print(core.parseDomHtml(gb.getEntry(), "demo2/Guestbook/guestbook.xsl"));
%>
With parseDomHTML HTML-tags - which would be escaped normally - will be shown.
New Entries
To make it possible to create a new guestbook entry - we first of all need a html-form:
guestbook.jsp line 10:
%> <h1>Neuen Eintrag erstellen</h1> <div class="whiteBox"> <div class="whiteBoxContent"> <form method="post"> Name: <input name="Guestbook_userName"><br/> Nachricht: <textarea name="Guestbook_message"></textarea><br/> <input type="submit" name="Guestbook_createEntry" value="Eintragen"> </form> </div> </div> <%@include file="../../includes/bottom.ijsp"%>
Next we have to call the function createEntry of the guestbook module:
guestbook.jsp line 7:
<%
Guestbook gb = new Guestbook(core);
gb.guestbookView.moduleName = "global";
gb.createEntry();
out.print(core.parseDomHtml(gb.getEntry(), "demo2/Guestbook/guestbook.xsl"));
Now one can already post new entries for the guestbook.
But there are still 3 problems to be resolved:
- Each time the page is realoaded, the same entry will be created again.
- For users who are logged in - the field name will be ignored
- A guest has the possibility to change his name and is therefor able to pose as someone else
Prevent multipostings
This could be done, by calling the function createEntry at another jsp-file, which afterwards forwards back to the real guestbook page. We do something similar, but much more easy to implement - we simply reload the page:
guestbook.jsp line 7:
<%
Guestbook gb = new Guestbook(core);
if(gb.createEntry()!=null)
{
response.sendRedirect("guestbook.jsp");
return;
}
out.print(core.parseDomHtml(gb.getEntry(), "demo2/Guestbook/guestbook.xsl"));
%>
The function createEntry returns only a value, when it is called. We check this and forward the browser simply to another page (in our case again guestbook.jsp but without the form-data).
Show the name-field only for guests
guestbook.jsp line 19:
<form method="post"> <% if (!core.isLoggedOn()) { %> Name: <input name="Guestbook_userName"><br/> <% } %> Nachricht: <textarea name="Guestbook_message"></textarea><br/>
Now the field name won't be shown for users who are logged in.
Identify entries from guests
A guest hast the user-id 1. We use this information, to display "Guest" and therefore identify a user as a guestuser.
guestbook.xsl line 5:
<h1> <xsl:if test="CREATE/USER/ID=1"> Gast: </xsl:if> <xsl:value-of select="CREATE/USER/NAME"/> </h1>
Now no guest can hide behind a false username.
The final guestbook.jsp
<%@include file="../../includes/header.ijsp"%> <% relpath = "../../"; %> <%@include file="../../includes/top.ijsp"%> <h1>DAS GäSTEBUCH</h1> <% Guestbook gb = new Guestbook(core); gb.guestbookView.moduleName = "global"; if(gb.createEntry()!=null) { response.sendRedirect("guestbook.jsp"); return; } out.print(core.parseDomHtml(gb.getEntry(), "demo2/Guestbook/guestbook.xsl")); %> <h1>Neuen Eintrag erstellen</h1> <div class="whiteBox"> <div class="whiteBoxContent"> <form method="post"> <% if (!core.isLoggedOn()) { %> Name: <input name="Guestbook_userName"><br/> <% } %> Nachricht: <textarea name="Guestbook_message"></textarea><br/> <input type="submit" name="Guestbook_createEntry" value="Eintragen"> </form> </div> </div> <%@include file="../../includes/bottom.ijsp"%>
The final guestbook.xsl
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:for-each select="GUESTBOOK/ENTRY"> <h1> <xsl:if test="CREATE/USER/ID=1"> Gast: </xsl:if> <xsl:value-of select="CREATE/USER/NAME"/> </h1> <div class="whiteBox"> <div class="whiteBoxContent"> <xsl:value-of select="MESSAGE"/> </div> </div> <div class="smallRight"> am <xsl:value-of select="CREATE/DATE"/> </div> <br clear="all" /> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Attachments
- guestbook1.png (15.4 kB) - added by niko 3 years ago.
- guestbook2.png (1.8 kB) - added by niko 3 years ago.
- guestbook3.png (2.4 kB) - added by niko 3 years ago.
- guestbook4.png (24.8 kB) - added by niko 3 years ago.
- guestbook5.png (9.2 kB) - added by niko 3 years ago.
- guestbook6.png (6.8 kB) - added by niko 3 years ago.
- guestbook7.png (3.1 kB) - added by niko 3 years ago.
- guestbook8.png (4.5 kB) - added by niko 3 years ago.
- guestbook9.png (3.3 kB) - added by niko 3 years ago.
- guestbook10.png (4.5 kB) - added by niko 3 years ago.
- guestbook11.png (3.8 kB) - added by niko 3 years ago.











