|
|
|
Response rendering
An HTTP response body can be generated using direct APIs or indirect rendering. In either case, you should set status and headers before writing any output. The following sections of this article provide information about response rendering:
Direct APIs
Application code and event handlers can write directly to the response using OutputStream (/request/outputstream) and PrintWriter (/request/writer) objects.
The following is a Java example:
// Content of a Java class
PrintWriter writer = (PrintWriter) zget("/request/writer");
writer.println("Hello World.");
The default output for all Groovy scripts (including templates) is the response writer. This allows application developers to write directly to the response with the println method, as shown in the following Groovy example:
println "Hello World."
Indirect rendering
Zero provides a library of renderers for common response handling. Invoking a renderer generally involves the following steps:
- Set the value of
/request/view in the Global Context to the corresponding renderer
- Set additional renderer-specific values into the GlobalContext, where appropriate
- Invoke zero.core.views.ViewEngine.render()
zero.core includes View, Error, JSON, and XML renderers.
NOTE: The JSON and XML renderers use reflection when serializing Objects. Using String, Number, Boolean, or primitive types as keys, values and element names will result in faster rendering.
View renderer
The View renderer serves a file from the app/views virtualized directory. Files are processed in a similar fashion to standard file serving in Zero, meaning executable files are "invoked" and static files are served directly.
To use this renderer, set /request/view to the file path relative to app/views.
The following is a Groovy example, specifying app/views/x/hello.gt as the view script:
request.view="x/hello.gt"
render()
The following is the equivalent PHP example, specifying app/views/x/hello.php as the view script:
<?php
zput('/request/view', 'x/hello.php');
render_view();
?>
Error renderer
The Error renderer is tailored for error reporting and uses scripts from the app/errors virtualized directory. zero.core includes a set of basic error pages; zero.core.webtools includes error pages that are more descriptive for developers.
To use this renderer, set /request/view to "error" and set /request/error/page to the path to the script relative to app/errors.
Groovy example:
request.view = "error"
request.error.page = "errorError"
render()
See the HTTP error handling documentation for more details.
JSON renderer
The JSON renderer serializes Java objects to the outputstream as JSON representations. Details about this serialization may be found on the JSON support documentation.
To use this renderer, set /request/view to "JSON" and set /request/json/output to the object to be serialized. The renderer will set the Content-Type response header to application/json, if not already set.
Groovy example:
Customer customer = new Customer();
customer.name = "John Smith"
request.view="JSON"
request.json.output=customer
render()
JSON rendering can be performed in PHP in one of two ways :
a) The following code sample shows how this can done using the ViewEngine
<?php
$customer = array('name' => 'John Smith');
zput('/request/view', 'JSON');
zput('/request/json/output', $customer);
render_view();
?>
b) Alternately, you can use the JSON extension to render the data directly
<?php
$customer = array('name' => 'John Smith');
echo json_encode($customer);
?>
XML renderer
The Extensible Markup Language (XML) renderer serializes Java objects to the outputstream as XML.
To use this renderer, set /request/view to "XML" and set /request/xml/output to the object to be serialized. It should be noted that if the object to be serialized is a subtype of org.w3c.dom.Node interface (such as Document or Element), the resulting XML will be the contents of the Node object (which is what you want), not a XML rendering of the Node Java object.
You can optionally set /request/xml/rootElement to the name of the XML tag that will form the root element of the produced XML document. If the /request/xml/rootElement value is not specified then the lower cased simple name of the Java Object will be used as the root element name.
You can optionally set /request/xml/stylesheet to the name of an XSL stylesheet located in the app/views folder of the project (or its dependencies). This will cause the stylesheet to be applied to the resulting XML document and the XSL output to be sent to the client. The value of the /request/xml/stylesheet attribute is a String relative path to the XSL document within the app/views folder.
The renderer will set the Content-Type response header to application/xml if not already set. Be careful when using the stylesheet attribute to set the Content-Type to something appropriate to the XSL output as the value of application/xml may be incorrect.
Groovy example (Object to XML):
Customer customer = new Customer();
customer.name = "John Smith"
request.view="XML"
request.xml.output=customer
// the root element name is optional
request.xml.rootElement='customer'
render()
Expected output:
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<name>John Smith</name>
</customer>
Groovy example (Using a XSL stylesheet):
Customer customer = new Customer();
customer.name = "John Smith"
request.view="XML"
request.xml.output=customer
// the root element name is optional
request.xml.rootElement='customer'
request.xml.stylesheet='viewCustomer.xsl'
render()
Java Example (Object to XML):
Customer customer = new Customer();
customer.setName("John Smith");
zput("/request/view","XML");
zput("/request/xml/output", customer);
zput("/request/xml/rootElement","customer");
zero.core.views.ViewEngine.render();
Also, if you may have circular references in your resulting XML document, you may want to consider setting /request/xml/idRefs to true. This will set the idref attribute to the respective id value. If a circular reference is detected and /request/xml/idRefs is null or set to anything other than true, then the idref value will be set to CIRCULAR_RFERENCE.
Groovy example (Object to XML):
Customer customer = new Customer();
customer.name = "John Smith"
request.view="XML"
request.xml.output=customer
// the root element name is optional
request.xml.rootElement='customer'
request.xml.idRefs = true
render()
Expected output:
<?xml version="1.0" encoding="UTF-8"?>
<customer id="1">
<name id="2">John Smith</name>
</customer>
Advanced Collection and Array item elements will contain an index attribute.
XML rendering can be performed in PHP in one of two ways:
a) The following code sample shows how this can done using the ViewEngine
<?php
$customer = array('name' => 'John Smith');
zput('/request/view', 'XML');
zput('/request/xml/output', $customer);
zput('/request/xml/rootElement', 'customer');
zput('/request/xml/idRefs', false);
render_view();
?>
b) Alternately, you can use the XML extension to render the data directly
<?php
$customer = array('name' => 'John Smith');
echo xml_encode($customer, false, 'customer');
?>
|