Re: How to Use Custom Bean in Freemarker
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
You just put that Users (sic) object into the data model, and then
access its properties like:
${users.name}
This works with the default wrapper too, since that extends
BeansWrapper. (Although I would avoid the default wrapper in new
projects, and just use pure BeansWrapper... but that's another story.)
--
Best regards,
Daniel Dekany
Wednesday, November 28, 2012, 5:04:47 PM, Dinesh Gupta wrote:
> Please help me out this problem.
> How can I access the POJO(custom bean) I Freemarker.
> How can I use BeansWrapper or other DataModal. Please give me an example of the one
>
>
> ----------------------------------
> import java.io.File;
> import java.io.FileWriter;
> import java.io.IOException;
> import java.io.OutputStreamWriter;
> import java.io.Writer;
> import java.util.ArrayList;
> import java.util.Calendar;
> import java.util.List;
>
> import org.apache.log4j.Logger;
>
>
>
> import freemarker.template.Configuration;
> import freemarker.template.ObjectWrapper;
> import freemarker.template.SimpleHash;
> import freemarker.template.Template;
> import freemarker.template.TemplateException;
>
>
> public class Main {
> private static final Logger LOGGER = Logger.getLogger(Main.class);
> public static void main(String[] args) {
> LOGGER.info("Entering main in Main");
> Calendar now = Calendar.getInstance();
> System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
> + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
>
> // add days to current date using Calendar.add method
> now.add(Calendar.DATE, 1);
>
> System.out.println("date after one day : "
> + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
> + "-" + now.get(Calendar.YEAR));
> sample();
> }
> private static void sample(){
> try {
> LOGGER.info("Entering sample in Case Manager");
> Configuration fmCfg = new Configuration();
> /*BeansWrapper beanWrapper = new BeansWrapper();
>
> //beanWrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
> beanWrapper.setExposeFields(true);
> fmCfg.setObjectWrapper(beanWrapper);*/
> Template template =
> fmCfg.getTemplate("./resources/helloworld.ftl");
> SimpleHash context = new
> SimpleHash(ObjectWrapper.BEANS_WRAPPER);
>
> // Build the data-model
> context.put("message", "Hello World!");
>
> //List parsing
> List<String> countries = new ArrayList<String>();
> countries.add("India");
> countries.add("United States");
> countries.add("Germany");
> countries.add("France");
> context.put("countries", countries);
> List<Users> lUsers = new ArrayList<Users>(2);
> lUsers.add(new Users("Diinesh","GatewayPark"));
> lUsers.add(new Users("Sandeep","Banglore"));
> context.put("users",new Users("Diinesh","GatewayPark"));
>
> // Console output
> Writer out = new OutputStreamWriter(System.out);
> template.process(context, out);
> out.flush();
>
> // File output
> Writer file = new FileWriter(new
> File("./output/FTL_helloworld.html"));
> template.process(context, file);
> file.flush();
> file.close();
> } catch (IOException e) {
> LOGGER.error(e);
> } catch (TemplateException e) {
> LOGGER.error(e);
> }
> }
> }
> class Users{
> String name;
> String place;
> public Users(String name, String place) {
> this.name = name;
> this.place = place;
> }
> public String getName() {
> return name;
> }
> public void setName(String name) {
> this.name = name;
> }
> public String getPlace() {
> return place;
> }
> public void setPlace(String place) {
> this.place = place;
> }
>
> }
> ----------------------------------
------------------------------------------------------------------------------
Keep yourself connected to Go Parallel:
INSIGHTS What's next for parallel hardware, programming and related areas?
Interviews and blogs by thought leaders keep you ahead of the curve.
http://goparallel.sourceforge.net