JSF與ajax使用h:messages 在commandButton上互動的問題以及在JSF前端使用ENUM資料型別
阿新 • • 發佈:2019-02-03
Ok, here is the final way:- Register the standard enum converter in faces-config.xml (optional):
<converter><converter-for-class>java.lang.Enum</converter-for-class><converter-class>javax.faces.convert.EnumConverter</converter-class></converter>
Add a function for example to a managed bean which converts the Enum values to an array of SelectItems:
@ManagedBeanpublicclassGenderBean{publicSelectItem[] getGenderValues(){SelectItem[] items =newSelectItem[Gender.values().length];int i =0;for(Gender g:Gender.values()){
items[i++]=newSelectItem(g, g.getLabel());}return items;}}
Then bind this function to the selectOneMenu in JSF:
<h:selectOneMenu id ="gender" value="#{person.gender}"><!-- use property name not method name --><f:selectItems value="#{genderBean.genderValues}"/></h:selectOneMenu>
That's it! Not the first explanation for this problem on the net. But i think it's the easiest & shortest one ;)
對於enum需要轉換器支援,自己寫一個轉換器吧