这里需要注意一点,commandbutton这个控件是html的input控件,会导致表单提交,本来想实现一个登录验证功能,点击登录按钮对用户名和密码进行验证,如果失败,在h:messages上显示错误消息,但是,添加的消息并不能显示,其原因就是在于commandbutton这个控件会提交表单,相当于刷新上下文,所以,添加的消息也就失效了,
可以ajax关联一个输出控件,这个输出控件绑定bean的一个值,在按钮验证的时候,如果验证失败,设置这个bean值,在返回空,就可以了,
还有一个就是jsf中使用enum类型的问题,有个帖子很好
http://stackoverflow.com/questions/2868778/jsf-2-0-use-enum-values-for-selectonemenu
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:
@ManagedBean public class GenderBean { public SelectItem[] getGenderValues() { SelectItem[] items = new SelectItem[Gender.values().length]; int i = 0; for(Gender g: Gender.values()) { items[i++] = new SelectItem(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 ;)