tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Jakarta-tapestry Wiki] Update of "CreatingCustomTranslators" by AlanChandler
Date Mon, 03 Oct 2005 19:26:12 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-tapestry Wiki" for change

The following page has been changed by AlanChandler:

New page:
Creating a Translator is very similar to CreatingCustomValidators.

= 1. Extend Abstract Translator =

The important part is to implement the formatObject and parseText methods to convert between
your object and string.

In my example I have a boolean object that is the gender (aka sex) of someone represented
by the 1 character strings M and F, defaulting to M when undefined. 

package uk.org.chandlerfamily.tapestry.translators;

//TODO: Rename Sex to Gender - change hivemodule.xml and useage in Create

import java.util.Locale;

import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.form.translator.AbstractTranslator;
import org.apache.tapestry.valid.ValidatorException;

public class Sex extends AbstractTranslator {
	private String _Sex;
	public Sex() {

	public Sex(String initializer) {

	public void setSex (String sex) {
		_Sex = sex;
	public String getSex () {
		return _Sex;

	public String formatObject(IFormComponent arg0, Locale arg1, Object arg2) {
		if (arg2 == null) return "M";
		return (Boolean) arg2 ? "M" : "F";

	public Object parseText(IFormComponent field, ValidationMessages messages,
			String text) throws ValidatorException {
		String [] args = new String[1];
		args[0] = text;
		if (!text.equalsIgnoreCase("M") && !text.equalsIgnoreCase("F")) {
			throw new ValidatorException(messages.formatValidationMessage(null, "invalid-format", args));

		return text.equalsIgnoreCase("M");



Note: I haven't managed to figure out how to create new message keys.  The invalid-format
message key is a standard tapestry one.

= 2. Configure the translator in hivemodule.xml =

Use the contribution element, with a bean element inside.  You can put more than one bean
element inside if you have multiple translators

<contribution configuration-id="tapestry.form.translator.Translators">
	<bean name="sex" class="uk.org.chandlerfamily.tapestry.translators.Sex" />

= 3. Use in your templates =

use the translator:beanName to access the translator - with the value parameter being the
translated object type.

			<td><span jwcid="@FieldLabel"
				field="component:gender" >Gender:</span></td>
			<td><input  jwcid="gender@TextField" 
				displayName="Gender" /></td>

and in your class - see how I have used a real method in an abstract class to create a default
value of true for this parameter (not sure if this is the correct way to do it)

public abstract class Create extends BasePage  {
	private boolean _sex=true;  //gives a default value
	public boolean getSex() {
		return _sex;
	public void setSex(boolean sex) {

To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org

View raw message