Java Server Faces(JSF)___转换器,converter标记实现,实现Converter接口
前言:
这个内容更加简单,根据自己的需要选择对应的标记来进行转换就好。
转换器一般用于UI组件相关联;
1.使用标准转换器
- 主要的标记有:
<c:convertNumber>标记 <c:convertDateTime>标记
2.实质:就是跟UI组件搭配使用,常用来处理用户输入的数据;
-
比如:将用户输入的数值转换为货币类型
<h:inputText value="#{beanName.property}"> <c:convertNumber type="currency" currencySymbol="€" /> </h:inputText>
-
再比如:将用户输入的数值转换为日期时间类型
<h:inputText value="#{beanName.property}"> <c:convertDateTime pattern="dd-MM-yyyy" /> </h:inputText>
3.创建自定义转换器:
注:除了时间和日期以外的标准转换器,你还可以创建自定义转换器;
-
实现步骤
(1).实现 Converter 接口,重写 getAsObject() 和 getAsString() 方法;
(2).将该类注册为自定义转换器 ;
(3).与 UI组件 相关联 ;object getAsObject(FaceContext context,UIComponent component,String newValue):此方法将用户输入的 字符串 转换为 对象 ;
object getAsString(FaceContext context,UIComponent component,Object value):此方法将用户输入的 对象 转换为 字符串; -
定义自定义类 (bean)
public class myConverter implements Converter{ public myConverter() { // TODO Auto-generated constructor stub } public Object getAsObject(FaceContext context,UIComponent component,String newValue) throws ConverterException { //逻辑代码放在这; } public Object getAsObject(FaceContext context,UIComponent component,Object value) throws ConverterException{ //return value.toString(); }
-
自注册成为自定义转换器(有两种方式供选择)
第一种:在 face.config.xml 中进行配置
<converter> <converter-id></converter-id> //自己定义的id,用于跟 UI 组件相关联; <converter-class></converter-class> //自己定义的类名; </converter>
第二种:在类之前添加批注@FacesConverter( value = " 自己定义的id,用于跟UI 组件绑定");使用批注时,需要导入相应的包;
@FacesConverter( value = " 自己定义的id,用于跟UI 组件绑定");
-
将转换器添加到 UI 组件
1.使用用户界面的converter属性;<h:inputText value="#{beanName.property}" converter="converter.id"></h:inputText>
2.使用< converter >标记
<h:inputText value="#{beanName.property}"> <c:converter converterId="converter.id" /> </h:inputText>
4.案例(将用户输入的16位电话号码,每四位后面添加一个连接符 " - ",比如用户输入的是0000111122223333,使用了验证器转换以后的值为0000-1111-2222-3333)
-
实现Converter接口
public class myConverter implements Converter{ public myConverter() { // TODO Auto-generated constructor stub } public Object getAsObject(FaceContext context,UIComponent component,String newValue) throws ConverterException { String cNumber = newValue.trim(); int len = cNumber.length(); if(len != 16){ FacesMessage message = new FacesMessage(); message.summary("Invalid Card Number!"); }else{ String part1 = cNumber.substring(0,4); String part2 = cNumber.substring(4,8); String part3 = cNumber.substring(8,12); String part4 = cNumber.substring(12,16); StringBuilder sb = new StringBuilder(); sb.append(part1 + "-"); sb.append(part2 + "-"); sb.append(part3 + "-"); sb.append(part4); return sb.toString(); } } public Object getAsObject(FaceContext context,UIComponent component,Object value) throws ConverterException{ return value.toString(); }
-
将该类注册为自定义转换器在 (* 在face.config.xml 中进行配置* )
<converter> <converter-id>myConverter</converter-id> //自己定义的id,用于跟 UI 组件相关联; <converter-class>myConverter</converter-class> //自己所编写的类的名称; </converter>
-
将自定义转换器跟UI组件相关联
<h:inputText value="#{beanName.property}" converter="myConverter"></h:inputText>
注:这里只是用了第一种方式来进行自定义转换器的应用,第二种方式也很简单,这里就不在举例。
更详细的内容,请查看:
https://blog.csdn.net/u012889434/article/details/50635891