Creating web pages using FreeMarker

Some people find writing XML code to create HTML counterintuitive. For those who choose not to use OFBiz widgets but would rather write HTML directly using FreeMarker, this section is for you.

Getting ready

The following prerequisites are needed to be taken care of:

  1. At a minimum, you will need to define a Screen widget with a widgets element declaration pointing to your FreeMarker template file.
  2. For any required data preparation, add declarations to the actions portion of the Screen widget definition.
  3. If needed, create any Groovy or BeanShell scripts in preparation for screen rendering.
  4. Open an existing FreeMarker file or create a new one.

How to do it...

Add HTML and/or FreeMarker directives to your file. For example, a simple web page that displays "Hello World" might have the following content:

<html xmls="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet"
href="<@ofbizContentUrl>/images/default.css</@ofbizContentUrl>"
type="text/css"/>
</head>
<body>
<h1>Hello World </h1>
</body>
</html>

Where ~/images/default.css is an external style sheet that contains styles for this HTML document.

How it works...

While OFBiz widgets are designed to prepare data and create HTML within the context of a single widget definition, FreeMarker provides a much cleaner separation of data preparation from HTML authoring. The OFBiz screen handler merges data from Screen widget actions into HTML using FreeMarker template directives.

FreeMarker templates consist of a procedural-oriented declaratives (if/then/else) mixed with HTML that allow for considerable control over how HTML web pages will be rendered. Many times when building OFBiz webapps using FreeMarker, you will need access to information about the execution context or current user session. The following tips show how to access context information while working within a FreeMarker file:

Tip

To access HTTP/HTTPS request attributes set from the Screen widget, OFBiz Services, OFBiz Events, or any other process:

<#assign x = requestAttributes._SOME_REQUEST_ATTRIBUTE_?has_content/>

To access HTTP/HTTPS request parameters from a URL, HTML form:

<#assign y = requestParameters.SOME_REQUEST_PARAMETER?has_content/>

Servlet context session attributes as set from another web page, OFBiz Screen widget, OFBiz Service, OFBiz Event, or other:

<#assign z = sessionAttributes.SOME_SESSION_ATTRIBUTE?has_content/>

Note: Context information passed from the Screen widget and from backend applications, for example error messages, are usually passed as HTTP/HTTPS request attributes.

There's more

FreeMarker transformations are similar to macros in that they are shortcuts that convert wrapped values in some predetermined fashion. The OFBiz folks have pre-built and supply with the distribution a number of useful FreeMarker transformations.

For example, to easily build the HTML hyperlink for an image, external CSS file, or JavaScript file, wrap the hyperlink URL within the following transformation tags:

<@ofbizContentUrl></@ofbizContentUrl>

This transform creates a link based on URLs relative to the entire OFBiz instance.

To create a navigation hyperlink to a URL relative to a runtime instance of an OFBiz webapp, enclose the URL in the following transformation directives:

<@ofbizUrl></@ofbizUrl>

Using this transform not only relieves the author from knowing where URLs are relative to the framework and/or currently executing web application, but they also perform any necessary SSL translations automatically.

See also

For more information about FreeMarker, please see the FreeMarker manual located at:

http://FreeMarker.sourceforge.net/docs/index.html

..................Content has been hidden....................

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