Mentawai Web Framework

Data Lists

A web application may have one or more static data lists that it has to show and control. For example, the list of states, a list of genders, a list of accepted payment methods, etc.

In mentawai, a data list is just an i18n file that gets loaded and controlled automatically for you. Mentawai also takes care of sorting, so you don't have to waste any time implementing those boring tasks. You can click here to download a war file with an example application that illustrates the use of data lists.

By default, mentawai will look inside the /lists directory and load all i18n files it finds in there as lists. The name of the list will be the name of the i18n file. The default sorting is by value.

Then you can use the org.mentawai.list.ListManager class to access these lists. The lists returned by ListManager.getList() are org.mentawai.list.ListData, which has some useful methods to get list items. List items are org.mentawai.list.ListDataItem, which has only two methods getId() and getValue().

A piece of code will explain better:

					
	ListData list = ListManager.getList("genders"); // return a ListData with the name "genders"
	
	// A ListData object will contain a list for each supported locale...
	// Therefore to fetch the list you must specify a locale...
	List<ListItem> items = list.getValues(LocaleManager.getDefaultLocale());
	
	Iterator<ListItem> iter = items.iterator();
	
	while(iter.hasNext()) {
	
		ListItem item = iter.next();
		
		System.out.println("ID: " + item.getKey());
		
		System.out.println("VALUE: " + item.getValue());
	}
			

A ListData actually has a different list for each locale supported by the web application. That's why for getting the actually list of items (ListDataItems), you have to call the method getValues(locale) to return a list for the given locale. Below we list the ListData interface:

package org.mentawai.list;

import java.util.*;

public interface ListData {
	
	public String getValue(int id, Locale loc);
	public List getValues(Locale loc);
	public String getName();
	public int size();
	
}
	

The files genders_loc.i18n must be in the /lists directory of the document root. We will see in a minute how we can change this default directory.

###################################
# File: /lists/genders_en_US.i18n #
###################################

1 = Male
2 = Female
		

Note that the key for each value must be a number, not a string.



Select Tag for combo boxes

You may have noticed that lists go very well with html combo boxes. The Mentawai tag <mtw:select> will accept a list as an attribute and display its contents. Check the JSP below for an example:

<%@ taglib uri="/WEB-INF/tags/mentawai.tld" prefix="mtw" %>
<html>
<body>
<h1>Hello Data List!</h1>
<form action="HelloMentawai.mtw" method="post">
Type your username: <input name="fullname" size="25" />
Your gender: <mtw:select name="gender" list="genders" />
<input type="submit" value="Send Now">
</form>
</body>
</html>
	


More Control

You may alter the default behaviour of ListManager through the method loadLists() inside your ApplicationManager. For example, you may want to sort your list by ID instead of value, which is the default and you may want to alter the directory where the list files are located. The code below will give you an idea of how to do that:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
	public void loadActions() {
		// ...
	}
	
	public void loadLocales() {
		// ...
	}
	
	public void loadLists() throws IOException {
		ListManager.setListDir("/mylists");
		ListManager.init(); // load lists in /mylists
		
		ListData genders = new BaseListData("genders", BaseListData.ORDER_BY_ID);
		ListManager.addList(genders);
		
		ListData states = new BaseListData("states", BaseListData.ORDER_BY_VALUE, "/states");
		ListManager.addList(states);
	}
}
	

You can also implement the interface org.mentawai.list.ListData to create your own types of lists, for example, you may want to create a DBListData that loads your lists from a database.