Tapestry - Enum强制

时间:2013-01-31 14:47:21

标签: java enums tapestry encoder coercion

我目前正在努力将枚举作为Tapestry 5表单的一部分。 所以我跟着these explanations没有成功。的确,我收到了这个错误:

[...]
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
    at org.apache.tapestry5.util.EnumValueEncoder.toClient(EnumValueEncoder.java:24) ~[tapestry-core-5.3.3.jar:na]
[...]

所以这就是我在页面类中的内容:

@Property
private ScanMode scanMode

(ScanMode是枚举类型) .tml文件:

<t:radiogroup t:value="scanMode">
 <t:label for="recto">Recto</t:label>
 <input t:id="recto" type="radio" t:type="radio" t:value="literal:RECTO"/>
                            <br />
 <t:label for="verso">Recto/Verso</t:label>
 <input t:id="rectoverso" type="radio" t:type="radio" 
                          t:value="literal:RECTO_VERSO"/>
</t:radiogroup>

最后,我的ApplicationModule.java:

private static <T extends Enum> void add(Configuration<CoercionTuple> configuration, Class<T> enumType) {
    configuration.add(CoercionTuple.create(String.class, enumType, StringToEnumCoercion.create(enumType)));
}

public void contributeMasterDispatcher(OrderedConfiguration<Dispatcher> configuration, @InjectService("AccessController") Dispatcher accessController) {
    configuration.add(AccessController.class.getSimpleName(), accessController, "before:PageRender");
}

任何想法都将不胜感激!

1 个答案:

答案 0 :(得分:2)

http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/corelib/components/Radio.html

最简单的方法(在AppModule中没有转换器):

@Property
private ScanMode scanMode

public ScanMode getRecto(){ return ScanMode.RECTO; }
public ScanMode getRectoVerso(){ return ScanMode.RECTO_VERSO; }


<t:radiogroup t:value="scanMode" >
    <t:label for="recto">Recto</t:label>
    <t:radio t:id="recto"/>
    <br />
    <t:label for="rectoVerso">Recto/Verso</t:label>
    <t:radio t:id="rectoVerso"/>
</t:radiogroup>