您的位置:首页 > Web前端

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

2014-11-13 23:14 627 查看
这里需要注意一点,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需要转换器支持,自己写一个转换器吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐