Calling Java methods from FreeMarker

Many times, it is convenient to call an OFBiz Java utility or tool directly from within FreeMarker. The OFBiz FreeMarker implementation allows for calling Java methods (utilities and tools) directly from within FreeMarker directives.

How to do it...

Java methods may be called from FreeMarker in the following way:

  1. Open the FreeMarker template file.
  2. To call an OFBiz Java method, use the FreeMarker assign directive and be sure to use the full Java packaging notation for the method as shown here:
    <#assign value =
    Static["org.ofbiz.base.util.UtilProperties"]. getPropertyValue("general.properties", defaultUrl)>
    
  3. Close the file.

Note

Note: Changes to FreeMarker files are immediate. You do not need to restart OFBiz.

How it works...

The FreeMarker program is a Java Application (not to be confused with an OFBiz web application) that takes FreeMarker directives, HTML markup, and prepared data and creates HTML destined for a browser. FreeMarker runs within the same JVM as OFBiz and therefore has access to all the runtime tools and utilities on the CLASSPATH.

There's more...

To call an OFBiz Service (and use the UtilMisc.toMap utility) from within a FreeMarker file, use the following:

<#assign result =
dispatcher.runSync("aService",
Static["org.ofbiz.base.util.UtilMisc"].toMap("contextValue1",
someValue, "userLogin", userLogin))/>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.181