JSF与ajax使用h:messages 在commandButton上交互的问题以及在JSF前端使用ENUM数据类型

这里需要注意一点,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 ;)

对于enum需要转换器支持,自己写一个转换器吧

相关文章

$.AJAX()方法中的PROCESSDATA参数 在使用jQuery的$.ajax()方...
form表单提交的几种方式 表单提交方式一:直接利用form表单提...
文章浏览阅读1.3k次。AJAX的无刷新机制使得在注册系统中对于...
文章浏览阅读1.2k次。 本文将解释如何使用AJAX和JSON分析器在...
文章浏览阅读2.2k次。/************************** 创建XML...
文章浏览阅读3.7k次。在ajax应用中,通常一个页面要同时发送...