首先Converter要分为两类 进行介绍:
一、JSF本身自带的Converter(转换器)
JSF定义了一系列标准的转换器(Converter),支持8种基本类型数据转换
DateTime、Number可以使用<f:convertDateTime>、<f:convertNumber>标签进行转换
二、JSF 自定义转换器(Converter):
基本create过程如下:
1. Create a converter class by implementing javax.faces.convert.Converter interface.
例如:public class AuthorConverter implements Converter
2. Override both getAsObject() and getAsString() methods.
Override要使用@override然后开始填写,个人使用不求甚解的方式对这两个方法的由来进行了
一定的思考,如有不对请赐教:
在不思考低层硬件实现的情况下,我认为不论是网页中的输入还是控制台中的输入(这里指外部输入设备,例如键盘),
对于编译器等工具而言都应该是先以String类型进行处理的,然后根据需要再处理转换成int,Date,float...等类型
原因:因为对于计算机而言它无法确认我们输入的时候是想让它以string类型,或者int型又或是float等类型进行输入,
当然有时候我们需要对输入的对象进行分隔操作的时候,比如我们需要得到一系列作者的名字,但是我们不知道 有多少作者,
那么这个时候我们就应该用一个输入框进行输入然后获得一个string或者object对象,然后根据我们每个作者之间用逗号(,)
分隔来进行作者的分离;(描述有点混乱)
3. Assign an unique converter ID with @FacesConverter annotation.(在Converter的java代码中添加注解来
在JSF中注册该定义的转换器)
例如:@FacesConverter("authorConverter")
4. Link your custom converter class to JSF component via f:converter tag
代码实例:
java代码:
package biz;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
@FacesConverter("authorConverter")
public class AuthorConverter implements Converter{
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
//将String转为List arg2是获得的参数
//去掉空格
String ipt = arg2.replace(" ", "");
if(ipt.equals("")) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR, "输入有误", "不可为空");
arg0.addMessage("author", message);
throw new ConverterException(message);
}
List<String> result = new ArrayList<String>();
//
String[] authors = ipt.split(",");
if(authors.length == 0) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR, "输入有误", "多作者请用逗号间隔");
arg0.addMessage("author", message);
throw new ConverterException(message);
}
for (String s : authors) {
result.add(s);
}
return result;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
//将List转为String
@SuppressWarnings("unchecked")
List<String> authors = (ArrayList<String>)arg2;
StringBuilder sb = new StringBuilder();
for (String i: authors) {
sb.append(i + ",");
}
//去掉最后一个,
String result = sb.toString();
int index = result.lastIndexOf(",");
result = result.substring(0, index);
return result;
}
}
xhtml中的使用代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>添加图书</title>
</h:head>
<h:body>
<!-- 设置了表单,因此其中用到托管bean是是设置值 -->
<h:form id="addBookForm">
<h:panelGrid columns="3">
<h:outputLabel value="图书名称:"></h:outputLabel>
<h:inputText id="name" value="#{bookBean.name}"
required="true"></h:inputText>
<h:message for="name"></h:message>
<h:outputLabel value="图书书号:"></h:outputLabel>
<h:inputText id="isbn" value="#{bookBean.isbn}"
required="true">
<!-- <f:validateRegex pattern="ISBN[0-9]{13}"></f:validateRegex> -->
<f:validator validatorId="ISBNValidator"/>
</h:inputText>
<h:message for="isbn"></h:message>
<h:outputLabel value="作者:"></h:outputLabel>
<h:inputText id="authors" value="#{bookBean.authors}" required="true"
converter="authorConverter"></h:inputText>
<h:message for="authors"></h:message>
<h:outputLabel value="出版时间:"></h:outputLabel>
<h:inputText id="publishTime" value="#{bookBean.publishTime}"
required="true">
<f:convertDateTime pattern="yyyy-MM-dd"></f:convertDateTime>
</h:inputText>
<h:message for="publishTime"></h:message>
<h:outputLabel value="价格:">
</h:outputLabel>
<h:inputText id="price" value="#{bookBean.price}" required="true">
<f:convertNumber maxFractionDigits="1"></f:convertNumber>
</h:inputText>
<h:message for="price"></h:message>
<h:outputLabel value="分类:"></h:outputLabel>
<h:selectOneListbox id="mainType" value="#{bookBean.mainType}"
size="1" valueChangeListener="#{bookControl.changeCategory}"
onchange="submit()" immediate="true">
<f:selectItem itemValue="0" itemLabel="请选择"></f:selectItem>
<f:selectItem itemValue="1" itemLabel="计算机"></f:selectItem>
<f:selectItem itemValue="2" itemLabel="文学"></f:selectItem>
<f:selectItem itemValue="3" itemLabel="管理"></f:selectItem>
<f:selectItem itemValue="4" itemLabel="其他"></f:selectItem>
</h:selectOneListbox>
<h:message for="mainType"></h:message>
<h:outputLabel value="子分类:"></h:outputLabel>
<h:selectOneListbox id="subType" value="#{bookBean.subType}"
size="1">
<f:selectItem itemValue="请选择" itemLabel="请选择"></f:selectItem>
<c:forEach var="cat" items="#{bookBean.subCategories}"
varStatus="catCount">
<f:selectItem itemValue="#{cat}" itemLabel="#{cat}"></f:selectItem>
</c:forEach>
</h:selectOneListbox>
<h:message for="subType"></h:message>
<!--bookControl.add意思是调用BookControl中的add方法,使用了托管bean -->
<h:commandButton value="添加" action="#{bookControl.add}"></h:commandButton>
<h:commandButton value="取消" immediate="true"></h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
.