【问题标题】:Tapestry - Enum coercionTapestry - 枚举强制
【发布时间】:2013-01-15 17:09:28
【问题描述】:

我目前正在尝试将枚举作为 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");
}

任何想法将不胜感激!

【问题讨论】:

    标签: java enums tapestry encoder coercion


    【解决方案1】:

    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>
    

    【讨论】:

    • 感谢您的回答!确实这是一种做我想做的事情的方法,但对我来说它仍然是一种解决方法:-(假设你的枚举中有几十个值,为每个值实现一个 getter 不是一个可行的解决方案。但因为它回答了我以前的问题,我将其标记为正确答案。
    • 有关信息,我设法通过将 scanMode 更改为 String 并自己管理背面的转换来解决这个问题。
    • 您也可以使用 动态添加所有需要的收音机
    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多