开发者

Master page using freemarker template and J2EE like .net

开发者 https://www.devze.com 2023-02-25 09:24 出处:网络
I am plannin开发者_StackOverflowg to create a web application using J2ee, Spring 3.0n, Freemarker and jQuery.

I am plannin开发者_StackOverflowg to create a web application using J2ee, Spring 3.0n, Freemarker and jQuery.

My question is: is there any way to create master page with header and footer and included all Javascript files so that i can directly call that master page in all my page and save time to include all js file again and again?

Same as .Net provides concept of a master page, I want to create my own master page in Freemarker.


Basically you write a macro, let's call it masterTemplate.

[#macro masterTemplate title="defaultTitle"]
    <!DOCTYPE html
            PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <title>${title}</title>
        ... scripts, style sheets, meta information ...
    </head>
    <body>
     <div id="header">...</div>
     <div id="content">
       [#nested /]
    </div>
    <div id="footer>...</div>
    </body>
    </html>
[/#macro]

Then, you use this macro within your pages like this:

[#import "/path/to/masterTemplate.ftl" as layout /]

[@layout.masterTemplate title="My test page"]
    ...content goes here...
[/@layout.masterTemplate]

You achieve some sort of decorating technique by passing all relevant data from the page as attribute to the masterTemplate: See the title attribute. In the same way you can pass additional scripts and stylesheets.

This technique is shown here: Freemarker wiki

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号