Tuesday, 25 October 2011

How to create java object in Freemarker?

If you are using Freemarker integrated with struts, look at the class org.apache.struts2.views.freemarker.FreemarkerResult. In this class there is a method createModel(). There you have a list of objects inserted into model used in freemarker rendering.

  • Application - servlet context attributes hash model
  • JspTaglibs - jsp tag lib factory model
  • Request - request attributes hash model
  • Session - session attributes hash model
  • request - the HttpServletRequst object for direct access
  • response - the HttpServletResponse object for direct access
  • stack - the OgnLValueStack instance for direct access
  • ognl - the instance of the OgnlTool
  • action - the action itself
  • exception - optional : the JSP or Servlet exception as per the servlet spec (for JSP Exception pages)
  • struts - instance of the StrutsUtil class

In the ftl code you can use struts variable:

struts.bean("java.util.Date")
It will return object of this type, constructed with zero parameter constructor.

No comments: