HOWTO: Use Freemarker, SiteMesh, and Spring MVC(2)

Here's an example of the Freemarker files used with the configuration above:

/WEB-INF/freemarker/index.ftl (the home page content)
Important to note is that we are importing a custom Freemarker macro library (kzspring.ftl) from the classpath:modules folder on line 5, but if we needed to customize it for this site, we could have put it into the WAR file's /modules/ directory.

Code:
<#ftl strip_whitespace=true>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<#assign page=JspTaglibs["/WEB-INF/taglibs/sitemesh-page.tld"]>
<#assign decorator=JspTaglibs["/WEB-INF/taglibs/sitemesh-decorator.tld"]>
<#import "/kzspring.ftl" as k />
<html>
<head>
<title>Kazaam Home Page</title>
</head>
<body>
<p>This is the content of the Kazaam Home Page</p>
</body>
</html>

/decorators/main.ftd (The main SiteMesh decorator)

Code:
<#ftl strip_whitespace=true>
<#assign authz=JspTaglibs["/WEB-INF/taglibs/authz.tld"]>
<#import "/kzspring.ftl" as k />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="${currentlang!'en'}" />
${head}
</head>
<body class="sidebars">
<div id="wrapper">
	<div id="container" class="clear-block">
		<div id="header">
		</div><!-- header -->
		<div id="sidebar-left" class="sidebar">
		</div><!-- sidebar-left -->
		<div id="center">
			{body}
		</div><!-- center -->
		<div id="sidebar-right" class="sidebar">
		</div><!-- sidebar-right -->
	</div>
</div>
</body>
</html>

classpath:modules/cms/page-create.ftl - This is the Page creation screen for our Content Management System. If we need to customize it for a site, we would copy it to the site's /modules/cms folder.
Note that kzspring.ftl is a customization of Spring's spring.ftl , so it has all the Spring MVC form macros, etc. You won't be able to use this file exactly because it references our custom domain classes representing a Page (form.page.title, etc) but you get the idea.

Code:
<#ftl strip_whitespace=true>
<#import "/kzspring.ftl" as k />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><#if RequestParameters.pageId??>Edit<#else>Add</#if> ${form.page.type?cap_first}</title>
</head>
<body>
<h1>Create a New Page</h1>
<form action="${currentUriWithQuery!''}" method="post">
	<fieldset class="page-form">
		<div class="form-item">
			<@k.formLabel path="form.page.title" message="Title" cssErrorClass="label-error" required=true/>
			<@k.formInput path="form.page.title" attributes="size=\"70\"" />
			<div class="description">The title of the ${form.page.type}.  Will be displayed with the content</div>
		</div>
	</fieldset>
</form:form>
</body>
</html>

Note that in all the above Freemarker files, I imported the kzspring.ftl file by referencing it using an absolute path with a leading slash (/) - even though it is in the package modules/kzspring.ft - because our templateLoaderPaths set modules as a root, we don't need to include 'modules' in the import statement.

你可能感兴趣的:(spring,html,cms,mvc,freemarker)