信息来源 http://tapestrying.group.javaeye.com/group/topic/1361
本文主要是介绍如何自定义 translator。
首先需要了解translator的作用 :
translator用来服务端和客户端值的转换
客户端显示永远都是String,而传到服务器端我们希望是其他的类型,这是就要用到translator了.
由于当前T5提供了名为number,double,string,integer的tanslator,
假设你定义了名为date的translator,html template看起来应该像这样:
<input t:type="TextField" t:id="date" t:translate="translate:date" t:value="date" size="30" />
注意t:translate="translate:date"这个属性,这个date就需要我们自己定义 ,定义如下:
package org.opend.bogo.translator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.tapestry.Translator;
import org.apache.tapestry.ValidationException;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.internal.util.InternalUtils;
public class DateTranslator implements Translator<Date>
{
public Date parseClient(String clientValue, Messages messages) throws ValidationException
{
if (InternalUtils.isBlank(clientValue))
return null;
try
{
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(clientValue);
return date;
}
catch (ParseException ex)
{
throw new ValidationException(messages.format("number-format-exception", clientValue));
}
}
/**
* Converts null to the blank string, non-null to a string representation.
*/
public String toClient(Date value)
{
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
return value == null ? "" : format.format(value);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.tapestry.Translator;
import org.apache.tapestry.ValidationException;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.internal.util.InternalUtils;
public class DateTranslator implements Translator<Date>
{
public Date parseClient(String clientValue, Messages messages) throws ValidationException
{
if (InternalUtils.isBlank(clientValue))
return null;
try
{
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(clientValue);
return date;
}
catch (ParseException ex)
{
throw new ValidationException(messages.format("number-format-exception", clientValue));
}
}
/**
* Converts null to the blank string, non-null to a string representation.
*/
public String toClient(Date value)
{
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
return value == null ? "" : format.format(value);
}
}
定义完转换类后需要注册你的 translator
定义MyAppModule,登记这个Translator
package org.opend.bogo.modules;
import java.io.IOException;
import java.util.Date;
import org.apache.tapestry.Translator;
import org.opend.bogo.translator.DateTranslator;
.......
public class MyAppModule {
public static void contributeTranslatorDefaultSource(
MappedConfiguration<Class, Translator> configuration) {
configuration.add(Date.class, new DateTranslator());
}
public static void contributeTranslatorSource(
MappedConfiguration<String, Translator> configuration) {
configuration.add("date", new DateTranslator());
}
.....
}
import java.io.IOException;
import java.util.Date;
import org.apache.tapestry.Translator;
import org.opend.bogo.translator.DateTranslator;
.......
public class MyAppModule {
public static void contributeTranslatorDefaultSource(
MappedConfiguration<Class, Translator> configuration) {
configuration.add(Date.class, new DateTranslator());
}
public static void contributeTranslatorSource(
MappedConfiguration<String, Translator> configuration) {
configuration.add("date", new DateTranslator());
}
.....
}
在classes/META-INF/MANIFEST.MF启动这个Module
Manifest-Version: 1.0
Tapestry-Module-Classes: org.opend.bogo.modules.MyAppModule
OK,再试一次,看看页面OK了吧?