Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tapestry-枚举强制_Java_Enums_Tapestry_Encoder_Coercion - Fatal编程技术网

Java Tapestry-枚举强制

Java Tapestry-枚举强制,java,enums,tapestry,encoder,coercion,Java,Enums,Tapestry,Encoder,Coercion,我目前正在尝试获取枚举作为Tapestry 5表单的一部分。 所以我没有成功。事实上,我得到了这个错误: [...] 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

我目前正在尝试获取枚举作为Tapestry 5表单的一部分。 所以我没有成功。事实上,我得到了这个错误:

[...]
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]
[...]
下面是我在page类中的内容:

@Property
private 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");
}
private static void add(配置,类枚举类型){
add(强制tuple.create(String.class,enumType,StringToEnumCoercion.create(enumType));
}
public void contributeMasterDispatcher(OrderedConfiguration配置,@InjectService(“AccessController”)Dispatcher AccessController){
add(AccessController.class.getSimpleName(),AccessController,“before:PageRender”);
}
任何想法都将不胜感激

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

@属性
专用扫描模式扫描模式
public ScanMode getRecto(){return ScanMode.RECTO;}
public ScanMode getRectoVerso(){return ScanMode.RECTO_VERSO;}
直肠

右/右
谢谢您的回答!事实上,这是我想做的事情的一种方式,但对我来说,这仍然是一种解决办法:-(假设枚举中有几十个值,为每个值实现一个getter不是一个可行的解决方案。但是,由于它回答了我前面的问题,我将把它标记为正确答案。有关信息,我通过将scanMode更改为String并自行管理背面的转换来解决。您可能还可以动态添加我们将使用
@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>