I'm trying to use an enum with a h:selectOneMenu box in a JSF2 project.
What I've got so far:
Enum:
public enum MyType {
TEST,
ME;
}
Backing bean:
@ManagedBean
public class MyBean {
private MyType type;
public MyType[] getTypes {
return MyType.values;
}
public void setType(MyType type) {
this.type = type;
}
public MyType getType() {
return this.type;
}
}
xhtml page:
<h:selectOneMenu id="mySelection"
value="#{myBean.type}">
<f:selectItems value="#{myBean.types}" />
</h:selectOneMenu>
My problem is that the values are correctly displayed but they are not saved (I'm actually using it in a seam3 hibernate project). When I tried it with a custom validator, I saw that the setType method is called twice, the second time with null -> resulting that nothing is saved. Am I missing anything?
So far I've checked the following topics:
* jsf-2-0-use-enum-values-for-selectonemenu * jsf开发者_JAVA百科-2-0-use-enum-in-selectmany-menu * jsf-best-way-to-enum-internationalization-i18nThanks a lot for your help,
StephanYou should in this particular case not have the need for a custom converter at all. JSF has a builtin enum converter. A custom converter for enums is only mandatory when you have a List<SomeEnum>
instead of SomeEnum
as value. See also Use enum in h:selectManyCheckbox.
Apart from two compilation errors due to missing parentheses in two methods, the code posted as far looks fine and should work fine. Your problem is likely caused by something else which you omitted from the question for simplicity.
精彩评论