Here's a simple mechanism I like to use so that, by default, the template associated with an element is named the same as the element's class name. For example, an element defined in a class ViewPerson would automatically use the template ViewPerson.html. With this, all elements that extend the following BaseElement don't need to be configured for a template when this default is acceptable. Note that you can still override this default by injecting a different template.
Here is the BaseElement class:
import com.uwyn.rife.engine.Element;
import com.uwyn.rife.template.Template;
import com.uwyn.rife.tools.ClassUtils;
/**
* Base element.
*/
public class BaseElement extends Element
{
private Template m_template;
public Template getTemplate() { return m_template; }
public void setTemplate(Template p_template) { m_template = p_template; }
/**
* Called by the RIFE framework to initialize the element.
*/
public void initialize()
{
if (m_template == null)
{
try
{
m_template =
getHtmlTemplate(ClassUtils.simpleClassName(getClass()));
}
catch (Exception exc)
{
}
}
}
}
As I mentioned, you can still use a different template if the default is not what you want. For example:
<site>
<element implementation="your.rife.elements.ViewPerson"/>
<element id="AjaxViewPerson" url="/AjaxViewPerson" implementation="your.rife.elements.ViewPerson">
<property name="template">
<template type="xml">AjaxViewPerson</template>
</property>
</element>
</site>
In this example the URL /viewperson (the default) will use ViewPerson.html as a template, and /AjaxViewPerson will use the same element but output the results using AjaxViewPerson.xml.