Create a Mail POJO

In this chapter we will create a Mail POJO and build a UI for it.

The POJO

Suppose a Mail object(in package "samples") includes following properties:

Create first NUI template.

Now create a NUI template to show the properties of mail object.
First, create a new file named Mail.nui.xml in /nuitemplates/samples directory. Then type following code:

<?xml version = "1.0"?>
<showmail>
<template ignoreText="false">
	<table>
	<n:loop for="{'sender' , 'receiver' , 'createDate' , 'subject' , 'content'}" id="'propertyName'">
		<tr>
		<td><p:title bean="mail" property="propertyName"/>: </td>
		<td><p:value bean="mail" property="propertyName"/></td>
		</tr>
	</n:loop>
	</table>
</template>
</showmail>
This template iterate each property of Mail(using n:loop and a ognl list), and output it's title and value.
Eech attribute value of xml element is treated as a OGNL expression, if you want to write a const string, remember qouted it by ''.
Attribute ignoreText determine write textNode into output or not. Default value is true(ignored).

Include template in JSP

Now you should create a JSP to show this template.
NUI can run without Web environment.
In showmail.jsp, you should import NUI taglib.
<%@ taglib prefix="nui" uri="/nui" %>
Then prepare a context map:
<%
	Mail mail = new Mail () ;
	// set properties' value
	mail.setSender ("webwork@mail.nui.org") ;
	mail.setReceiver ("struts@mail.nui.org") ;
	mail.setCreateDate (new Date ()) ;
	mail.setSubject ("About NUI, a property oriented template engine.") ;
	mail.setContent ("message body.") ;
	HashMap context = new HashMap () ;
	context.put ("mail" , mail) ;
%>
You can write a NuiContextProvider to aviod use context map.
Then use tag template nui:template to call template.
<nui:template name="samples-showmail" context="<%= context %>"/>
NUI replaces '-' with '/' in Template name, add ".nui.xml" as template file path name. In this example, template named samples-showmail should defined in /nuitemplates/samples/showmail.nui.xml.
When you access this jsp, you get a table like this:
sender: webwork@mail.nui.org
receiver: struts@mail.nui.org
createDate: 2006/04/14
subject: About NUI, a property oriented template engine.
content: message body.

Next