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):
Add a function for example to a managed bean which converts the Enum values to an array of SelectItems:
Then bind this function to the selectOneMenu in JSF:
That's it! Not the first explanation for this problem on the net. But i think it's the easiest & shortest one ;)
对于enum需要转换器支持,自己写一个转换器吧
可以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使用json数据交互,出现中文乱码或因包含html标答而报错的问题(base64加密)
- Ajax、servlet、jsp、JSON数据交互以及跳转问题
- Intent传值问题(非Integer and String 数据类型)以及 intent使用方法
- 使用Bootstrap Tabs选项卡Ajax加载数据的实现以及遇到的问题;
- 当使用ajax 由前台像后台post 数组类型数据的一些问题(基于springboot)
- 【菜鸟学WCF】使用js+ajax调用WCF以及返回数据类型的控制
- 用enum类型数据解决switch case选择字符串的问题
- 使用AjaxPro与Session交互时遇到一个问题
- 使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- 转载---使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动
- 使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动
- Hibernate数据的lazy问题,以及使用spring整合hibernate lazy的解决办法(好文转载)
- 使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动
- J2EE下使用AJAX(六) 后记以及如何处理复杂的返回值类型
- Enum类型数据使用
- JavaScirpt和JSP数据数据交互使用问题
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- JSP数据和JavaScirpt数据交互使用问题的一种解决方法