加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSF与ajax使用h:messages 在commandButton上交互的问题以及在JSF

发布时间:2020-12-16 00:16:05 所属栏目:百科 来源:网络整理
导读:这里需要注意一点,commandbutton这个控件是html的input控件,会导致表单提交,本来想实现一个登录验证功能,点击登录按钮对用户名和密码进行验证,如果失败,在h:messages上显示错误消息,但是,添加的消息并不能显示,其原因就是在于commandbutton这个控件

这里需要注意一点,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需要转换器支持,自己写一个转换器吧

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读