You can't use generics in EL. EL is a runtime language based on reflection. You know, generics is only available during compiletime, not during runtime.
For your particular purpose, it's likely easier to use OmniFaces <o:importConstants>
.
<o:importConstants type="com.example.Item$Rarity" var="Rarity" />
...
<h:selectOneMenu>
<f:selectItems value="#{Rarity}" />
</h:selectOneMenu>
(the var
attribute is not mandatory, but you'd otherwise need to reference it as #{Item$Rarity}
which is not exactly nicely readable; if your Rarity
enum were a standalone enum and not an inner enum, then you could just use type="com.example.Rarity"
)
It's by design treated as a Map<String, Rarity>
, not a List<Rarity>
or so. So if you intend to access the individual items in the var
attribute of <f:selectItems>
, so that you can access specific enum methods, then you'd need to explicitly iterate over Map#values()
(which would require EL 2.2 support).
<h:selectOneMenu>
<f:selectItems value="#{Rarity.values()}" var="rarity" itemValue="#{rarity}" itemLabel="#{rarity.readableName}" />
</h:selectOneMenu>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…