2. Configuration in the friends application

Now that you know a bit more about participants you should feel comfortable about adding the configuration participant to the repository. We begin by editing the participants file.

Example 6.1. Participant file for Friends database

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rep SYSTEM "/dtd/rep.dtd">
    
<rep>
  <participant param="rep/config.xml">ParticipantConfig</participant>
  <participant param="sites/friends.xml">ParticipantSite</participant>
</rep>

The configuration participant takes a parameter specifying which file contains the configuration data, in this case rep/config.xml. The configuration file we are going to use looks like:

Example 6.2. A configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config SYSTEM "/dtd/config.dtd">

<config>
  <param name="DISPLAY_TITLE">Friends of Mikael</param>
</config>

The configuration parameter "DISPLAY_TITLE" will now be available from the element implementation. Now we just need to modify our template display.html by adding a value tag for the title. This tag can be used to set the title from our implementation.

Example 6.3. Display template modified to use configuration data for title

<html>
  <head><title><!--V 'title'-->My Friends<!--/V--></title></head>
  <body>
    <h3><!--V 'CONFIG:DISPLAY_TITLE'-->My Friends<!--/V--></h3>

    <!-- Rest of the template -->

  </body>
</html>

In Section 2, “The value tag” we mentioned that the string between the opening tag and the closing tag, here "My Friends", is the default value that will be used if we don't provide a value. The template in Example 6.3, “Display template modified to use configuration data for title” shows two different ways of setting the title. The first way is to access the configuration value directly with a template value tag:

<!--V 'CONFIG:DISPLAY_TITLE'-->

The other way is to access it from the Java implementation and use setValue to set it in the template.

Config config = Config.getRepInstance();
if (config.hasParameter("DISPLAY_TITLE"))
{
  template.setValue("title", config.getString("DISPLAY_TITLE"));
}

If the value is set in the configuration file it's used as title, otherwise the default string "My Friends" will be used.