Pages

Thursday, November 29, 2007

Freemarker with sample code


Freemarker:-

FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to auto generated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.

FreeMarker is designed to be practical for the generation of HTML Web pages, particularly by servlet-based applications following the MVC (Model View Controller) pattern. The idea behind using the MVC pattern for dynamic Web pages is that you separate the designers (HTML authors) from the programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become polluted with complex program fragments. This separation is useful even for projects where the programmer and the HTML page author is the same person, since it helps to keep the application clear and easily maintainable.

Although FreeMarker has some programming capabilities, it is not a full-blown programming language like PHP. Instead, Java programs prepare the data to be displayed (like issue SQL queries), and FreeMarker just generates textual pages that display the prepared data using templates



FreeMarker is not a Web application framework. It is suitable as a component in a Web application framework, but the FreeMarker engine itself knows nothing about HTTP or servlets. It simply generates text. As such, it is perfectly usable in non-web application environments as well. Note, however, that we provide out-of-the-box solutions for using FreeMarker as the view component of Model 2 frameworks (e.g. Struts), which also let you use JSP taglibs in the templates.

/**
* Sample code for generate freemarker template
*/


import java.io.File;
import java.io.StringWriter;
import freemarker.template.Template;
import freemarker.template.Configuration;
import org.apache.commons.io.FileUtils;
import com.custombeans.TemplateBean;
import com.helper.GenerateFileHelper;

class freemarkerDemo{

public static synchronized void generateTemplate() {
try {
Configuration config = new freemarker.template.Configuration().setServletContextForTemplateLoading(servletContext, "/WEB-INF/templates/");
Template tmpl =config.getTemplate("template.ftl");
StringWriter sw = new StringWriter();
Map bean = new HashMap();
bean.put("id",1);
bean.put("FirstName","Amin");
bean.put("LastName","Miral");
tmpl.process(bean, sw);

File resultFile = new File(CONTEXT.getRealPath("template.html"));
try {
FileUtils.writeStringToFile(resultFile, sw.getBuffer().toString(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


EDITOR / IDE - Freemarker
Eclipse 3.x Syntax highlight, syntax error indicator, outline view, code completion for macro names and bean property names.

IntelliJ IDEA 8.0 A well-known feature-rich commercial Java IDE.
jEdit 4.1 jEdit is a Free programmer's text editor written in Java. It is highly customizable, extensible, and has an easy-to-use interface.
SitePad Pro 6 SitePad Pro is an Integrated Development Environment (IDE) for Java, HTML, VRML and other files. It's commercial software.


Google
 

Java-Struts