Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Obtained from: JIRA Submitted by: rhallier Reviewed by: CVS: ---------------------------------------------------------------------- CVS: Issue number: CVS: If this change addresses one or more issues, CVS: then enter the issue number(s) here. CVS: Obtained from: CVS: If this change has been taken from another system, CVS: then name the system in this line, otherwise delete it. CVS: Submitted by: CVS: If this code has been contributed to the project by someone else; i.e., CVS: they sent us a patch or a set of diffs, then include their name/email CVS: address here. If this is your work then delete this line. CVS: Reviewed by: CVS: If we are doing pre-commit code reviews and someone else has CVS: reviewed your changes, include their name(s) here. CVS: If you have not had it reviewed then delete this line.
- Loading branch information
rhallier
committed
May 3, 2004
1 parent
65838bb
commit ecf3df6
Showing
20 changed files
with
330 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ velocity.log* | |
maven.log* | ||
clover | ||
*.i* | ||
.classpath | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<html> | ||
<head> | ||
<title>Freemarker Decorators</title> | ||
</head> | ||
|
||
<body> | ||
As of SiteMesh 2.0.2 <a href="http://freemarker.sourceforge.net/" target="_blank">Freemarker</a> (.ftl) | ||
decorators are supported. | ||
|
||
<p>Here is an example of how such a decorator might look like:</p> | ||
|
||
<pre style="border: 1px solid #999999; padding: 5px"> | ||
<#include "/includes/decorators/header.dec"> | ||
<h2><b>${title}</b></h2> | ||
<b>${head}</b> | ||
<img src="<b>${base}</b>/images/logo.gif" border="0"> | ||
<td valign="top" class="body"> | ||
<div class="header"> | ||
<span class="pagetitle"><b>${title}</b></span> | ||
</div> | ||
<b>${body}</b> | ||
</td> | ||
<#include "/includes/decorators/footer.dec"> | ||
</pre> | ||
|
||
<h3>Installation</h3> | ||
|
||
<ul> | ||
|
||
<li>Download <a | ||
href="http://freemarker.sourceforge.net/" target="_blank">Freemarker</a> 2.3 (recommended) and copy it into <b><code>[web-app]/WEB-INF/lib</code></b>. | ||
The SiteMesh distribution comes with <b>freemarker.jar</b> v2.3rc3</p></li> | ||
|
||
<li>Add the following to <b><code>[web-app]/WEB-INF/web.xml</code></b> | ||
within the <b><code><web-app></code></b> tag:</li> | ||
</ul> | ||
|
||
<pre style="border: 1px solid #999999; padding: 5px"> | ||
<servlet> | ||
<servlet-name>sitemesh-freemarker</servlet-name> | ||
<servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class> | ||
<init-param> | ||
<param-name>TemplatePath</param-name> | ||
<param-value>/</param-value> | ||
</init-param> | ||
<init-param> | ||
<param-name>default_encoding</param-name> | ||
<param-value>ISO-8859-1</param-value> | ||
</init-param> | ||
<load-on-startup>1</load-on-startup> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>sitemesh-freemarker</servlet-name> | ||
<url-pattern>*.dec</url-pattern> | ||
</servlet-mapping> | ||
</pre> | ||
|
||
<ul> | ||
<li>Modify <b><code>decorators.xml</code></b> to reference a .dec file.</li> | ||
</ul> | ||
|
||
<pre style="border: 1px solid #999999; padding: 5px"> | ||
<#include "/includes/decorators/header.dec"> | ||
<h2><b>${title}</b></h2> | ||
<b>${head}</b> | ||
<img src="<b>${base}</b>/images/logo.gif" border="0"> | ||
<td valign="top" class="body"> | ||
<div class="header"> | ||
<span class="pagetitle"><b>${title}</b></span> | ||
</div> | ||
<b>${body}</b> | ||
</td> | ||
<#include "/includes/decorators/footer.dec"> | ||
</pre> | ||
|
||
<h3>Context</h3> | ||
|
||
<p>FreemarkerDecoratorServlet puts some things into the context object that you should be aware of: </p> | ||
|
||
<h4>Basic context attributes</h4> | ||
<p> | ||
<li>It makes all request, request parameters, session, and servlet context attributes available to templates through Request, RequestParameters, Session, and Application variables. For example : | ||
<pre style="border: 1px solid #999999; padding: 5px">${Session["user"]}</pre> | ||
</li> | ||
|
||
<li>The scope variables are also available via automatic scope discovery. That is, writing Application.attrName, Session.attrName, Request.attrName is not mandatory; it's enough to write attrName, and if no such variable was created in the template, it will search the variable in Request, and then in Session, and finally in Application. </li> | ||
<pre style="border: 1px solid #999999; padding: 5px"> | ||
<#assign ww=JspTaglibs["/WEB-INF/webwork.tld"]></pre> | ||
</li> | ||
|
||
<li>It creates a variable with name JspTaglibs, that can be used to load JSP taglibs. For example: | ||
<pre style="border: 1px solid #999999; padding: 5px"> | ||
<#assign ww=JspTaglibs["/WEB-INF/webwork.tld"]> | ||
... | ||
<@ww.property value="myVar"/> </pre> | ||
</li> | ||
</p> | ||
|
||
<h4>Sitemesh context attributes</h4> | ||
<table border="0" cellspacing="10"> | ||
<tr> | ||
<td valign="top"><strong>base</strong></td> | ||
<td>request.getContextPath()</td> | ||
</tr> | ||
<tr> | ||
<td valign="top"><strong>title</strong></td> | ||
<td>Parsed page title (<title>...<title>)</td> | ||
</tr> | ||
<tr> | ||
<td valign="top"><strong>head</strong></td> | ||
<td>Parsed page head</td> | ||
</tr> | ||
<tr> | ||
<td valign="top"><strong>body</strong></td> | ||
<td>Parsed page body</td> | ||
</tr> | ||
<tr> | ||
<td valign="top"><strong>page</strong></td> | ||
<td>SiteMesh's internal Page object</td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<html> | ||
<head> | ||
<title>Velocity Decorator - ${title}</title> | ||
<link href="${base}/decorators/main.css" rel="stylesheet" type="text/css"> | ||
${head} | ||
</head> | ||
|
||
<body> | ||
<div id="pageTitle">${title}</div> | ||
<hr/> | ||
|
||
${body} | ||
|
||
<div id="footer"> | ||
<b>Disclaimer:</b> This site is an example site to demonstrate SiteMesh. It serves no other purpose. | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<head> | ||
<title>Freemarker Test</title> | ||
</head> | ||
<body> | ||
|
||
<p>This page is decorated by the FreemarkerDecoratorServlet.</p> | ||
|
||
<p>Go <a href="index.html">back</a>.</p> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/java/com/opensymphony/module/sitemesh/freemarker/FreemarkerDecoratorServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Title: FreemarkerDecoratorServlet Description: | ||
* | ||
* This software is published under the terms of the OpenSymphony Software License version 1.1, of which a copy has been included | ||
* with this distribution in the LICENSE.txt file. | ||
*/ | ||
|
||
package com.opensymphony.module.sitemesh.freemarker; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import com.opensymphony.module.sitemesh.HTMLPage; | ||
import com.opensymphony.module.sitemesh.RequestConstants; | ||
import com.opensymphony.module.sitemesh.util.OutputConverter; | ||
|
||
import freemarker.ext.servlet.FreemarkerServlet; | ||
import freemarker.template.SimpleHash; | ||
import freemarker.template.Template; | ||
import freemarker.template.TemplateModel; | ||
|
||
/** | ||
* Servlet that allows Freemarker templates to be used as decorators. | ||
* | ||
* @author <a href="mailto:richard.hallier@freesbee.fr">Richard HALLIER</a> | ||
* @version $Revision: 1.1 $ | ||
*/ | ||
public class FreemarkerDecoratorServlet extends FreemarkerServlet | ||
{ | ||
/* (non-Javadoc) | ||
* @see freemarker.ext.servlet.FreemarkerServlet#preTemplateProcess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, freemarker.template.Template, freemarker.template.TemplateModel) | ||
*/ | ||
protected boolean preTemplateProcess( | ||
HttpServletRequest request, | ||
HttpServletResponse response, | ||
Template template, | ||
TemplateModel templateModel) | ||
throws ServletException, IOException | ||
{ | ||
boolean result=super.preTemplateProcess(request, response, template, templateModel); | ||
|
||
SimpleHash hash = (SimpleHash) templateModel; | ||
|
||
HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE); | ||
|
||
String title, body, head; | ||
|
||
if(htmlPage==null) | ||
{ | ||
title="No Title"; | ||
body="No Body"; | ||
head="<!-- No head -->"; | ||
} | ||
else | ||
{ | ||
title=htmlPage.getTitle(); | ||
|
||
StringWriter buffer = new StringWriter(); | ||
htmlPage.writeBody(OutputConverter.getWriter(buffer)); | ||
body=buffer.toString(); | ||
|
||
buffer = new StringWriter(); | ||
htmlPage.writeHead(OutputConverter.getWriter(buffer)); | ||
head=buffer.toString(); | ||
|
||
hash.put("page",htmlPage); | ||
} | ||
|
||
hash.put("title",title); | ||
hash.put("body",body); | ||
hash.put("head",head); | ||
hash.put("base",request.getContextPath()); | ||
|
||
/* | ||
Factory factory = Factory.getInstance(new Config(getServletConfig())); | ||
Decorator decorator = factory.getDecoratorMapper().getDecorator(request, htmlPage); | ||
-> decorator.getPage() | ||
*/ | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.