我就是这样做的(在一个单独的 jar 文件中,并使用 maven 的默认目录):
1) 创建转换器的类(在 src/main/java 内)
2) 创建一个 .taglib.xml 类(在 src/main/resources/META-INF 内)
3) 创建 faces-config.xml (在 src/main/resources/META-INF 里面)
例子:
第1步)
package com.ocabit.jsf.converter;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
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;
import org.springframework.stereotype.Service;
import com.ocabit.utils.currency.CurrencyUtils;
/**
* Converter para valores BigDecimal.
*
* @author Carlos Eduardo Pauluk
*
*/
@FacesConverter("bigDecimalConverter")
@Service("bigDecimalConverter")
public class BigDecimalConverter implements Converter, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String CONVERTER_ID = "com.ocabit.jsf.converter.BigDecimalConverter";
/**
* Em caso de null, força a saída para 0.0;
*/
private boolean nullToZero = false;
/**
* Em caso de zero, força a saída para null;.
*/
private boolean zeroToNull = false;
/**
* Só retorna números positivos.
*/
private boolean onlyAbs = false;
/**
* Só retorna números negativos.
*/
private boolean onlyNeg = false;
/**
* Quantidade de casas decimais.
*/
private int decimals = 2;
@Override
public Object getAsObject(final FacesContext context, final UIComponent component, final String value) {
try {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(getDecimals(), RoundingMode.HALF_DOWN);
if (bd.equals(BigDecimal.ZERO) && isZeroToNull()) {
return null;
}
if (isOnlyAbs()) {
bd = bd.abs();
}
if (isOnlyNeg()) {
bd = bd.abs();
bd = bd.negate();
}
return bd;
} catch (final Exception e) {
BigDecimal bd = null;
if (isNullToZero()) {
bd = CurrencyUtils.getBigDecimalCurrency("0.0");
bd = bd.setScale(getDecimals(), RoundingMode.HALF_DOWN);
}
return bd;
}
}
@Override
public String getAsString(final FacesContext context, final UIComponent component, final Object value) {
if (!(value instanceof BigDecimal)) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao converter o valor decimal.", ""));
}
final NumberFormat nf = NumberFormat.getInstance();
// sempre terá 2 casas decimais
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
return nf.format(((BigDecimal) value).doubleValue());
}
public boolean isNullToZero() {
return nullToZero;
}
public void setNullToZero(boolean nullToZero) {
this.nullToZero = nullToZero;
}
public boolean isZeroToNull() {
return zeroToNull;
}
public void setZeroToNull(boolean zeroToNull) {
this.zeroToNull = zeroToNull;
}
public boolean isOnlyAbs() {
return onlyAbs;
}
public void setOnlyAbs(boolean onlyAbs) {
this.onlyAbs = onlyAbs;
}
public boolean isOnlyNeg() {
return onlyNeg;
}
public void setOnlyNeg(boolean onlyNeg) {
this.onlyNeg = onlyNeg;
}
public int getDecimals() {
return decimals;
}
public void setDecimals(int decimals) {
this.decimals = decimals;
}
}
第2步)
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"facelet-taglib_1_0.dtd">
<facelet-taglib version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">
<namespace>http://ocabit.com.br/facelets</namespace>
<tag>
<tag-name>convertBigDecimal</tag-name>
<converter>
<converter-id>com.ocabit.jsf.converter.BigDecimalConverter</converter-id>
</converter>
</tag>
</facelet-taglib>
步骤 3)
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<converter>
<description></description>
<converter-id>com.ocabit.jsf.converter.BigDecimalConverter</converter-id>
<converter-class>com.ocabit.jsf.converter.BigDecimalConverter</converter-class>
<property>
<property-name>nullToZero</property-name>
<property-class>boolean</property-class>
<description>Ao invés de retornar 'null', retorna '0.0'</description>
</property>
<property>
<property-name>zeroToNull</property-name>
<property-class>boolean</property-class>
<description>Ao invés de retornar '0.0', retorna 'null'</description>
</property>
<property>
<property-name>onlyAbs</property-name>
<property-class>boolean</property-class>
<description>Somente retorna números positivos</description>
</property>
<property>
<property-name>onlyNeg</property-name>
<property-class>boolean</property-class>
<description>Somente retorna números negativos</description>
</property>
<property>
<property-name>decimals</property-name>
<property-class>int</property-class>
<description>Quantidade de casas decimais</description>
</property>
</converter>
</faces-config>
瞧。