The Enhydra FAQ
How can I do server-side includes with Enhydra XMLC?
The answer from Lutris' Peter McLain is, "grab the new Enhydra
3.1 beta and do the following:"
Step 1: Create the standard enhydra application
$ newapp ssidemo
$ cd ssidemo
$ make
$ cd output
$ ./start
Hit http://localhost:9000 to prove the demo app works.
Step 2: Create the top banner include file, banner.ssi
The banner.ssi file is the Server Side Include that we want at
the top of all the pages. In this example, all we have is a name,
that should be either "Guest" (if they are not logged
in) or the user's real name (if they are logged in).
Create the file ssidemo/presentation/Banner.ssi:
<table>
<tr>
<td><span id="custName">Publius</span></td>
</tr>
</table>
Step 3: Create an interface wrapper for the include, Banner.html
Since all of our html files will <!--#include ... --> the
banner, they will all end up having a common subset of methods created
by XMLC (namely setTextCustName(String s) and getElementCustName()).
We'd like to capture this common subset behind an interface so that
we can write an object that will manipulate any page that has the
banner but only depends on an interface. There is one slight quirk
here. Since XMLC only accepts complete HTML pages, we write a Banner.html
that includes Banner.ssi. The only reason for this, is so we can
run it through XMLC to generate the common interface, BannerHTML.java.
Create the file ssidemo/presentation/Banner.html:
<html>
<head>
<title>Banner</title>
</head>
<body>
<!--#include virtual="Banner.ssi"-->
</body>
</html>
Step 4: Have Welcome.html include the banner
We want the Banner.ssi included in all of our pages, so we need
to edit Welcome.html to include it. In a real app, all/most/many
of the pages would include Banner.ssi, but here we only have one
page.
Edit ssidemo/presentation/Welcome.html and add the following as
the first row of the table:
<TR>
<TD>
<!--#include virtual="Banner.ssi"-->
</TD>
</TR>
Step 5: Fixup the makefile to tell XMLC about all of this
We need to tell XMLC to do two things:
A: Automatically create the BannerHTML interface.
This is done by having xmlc compile Banner.html (we add BannerHTML
to the HTML_CLASSES macro). We also tell xmlc to use server side
includes (-ssi) and to generate an interface (-generate interface).
All of this results in xmlc creating BannerHTML.java and compiling
it. The file (stripped of comments etc.) looks like:
public interface BannerHTML extends org.enhydra.xml.xmlc.html.HTMLObject
{
public org.w3c.dom.html.HTMLElement getElementCustName();
public void setTextCustName(String text);
}
B: For Welcome.html, turn on server side include processing and
have it impelment the BannerHTML interface. Since WelcomeHTML now
depends on BannerHTML, we make sure BannerHTML is compiled first.
Edit ssidemo/presentation/Makefile:
XMLC_Banner_OPTS = -ssi -generate interface
XMLC_Welcome_OPTS = -ssi -generate class -implements BannerHTML
HTML_CLASSES = BannerHTML WelcomeHTML
Step 6: Create a single pile of code to manipulate the banner.
We now write the java code that knows how to manipulate the banner.
Create ssidemo/presentation/Banner.java
package ssi.presentation;
import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
import org.enhydra.xml.xmlc.XMLObject;
public class Banner {
public void render(BannerHTML aBanner, HttpPresentationComms comms)
{
// do stuff to calculate the real name, e.g.,
// pull it out of session, grab it from a DB,
// whatever...or just call everyone Fred
aBanner.setTextCustName("Fred");
}
}
Step 7: Fixup the Welcome PO to call the Banner object:
We now need the Welcome.java code to call a Banner object to render
the page. This is a simple version. If your banner object ends up
being thread safe, you could just create one of them and have all
POs share it (save a bit in object creation etc.).
Edit ssidemo/presentation/Welcome.java to add the two new lines
of code:
package ssi.presentation;
import java.util.Date;
import com.lutris.xml.xmlc.*;
import com.lutris.appserver.server.httpPresentation.*;
public class Welcome implements HttpPresentation {
public void run(HttpPresentationComms comms)
throws HttpPresentationException {
String now = new Date().toString();
WelcomeHTML welcome = (WelcomeHTML)comms.xmlcFactory.create(WelcomeHTML.class);
welcome.setTextTime(now);
// Add these two lines of code to process the banner:
Banner b = new Banner();
b.render(welcome, comms);
comms.response.writeHTML(welcome);
}
}
Step 8: Fixup makefile to compile Banner.java:
Add the Banner class to the CLASSES marco
CLASSES = \
Redirect \
Banner \
Welcome
Step 9: Make and run the new application
$ cd <wherever>/ssidemo
$ make clean # Just to be safe
$ make
$ cd output
$ ./start
Hit http://localhost:9000 and notice that the name "Fred"
appears at the top.
|