每当我们使用输入文本作为十进制值时,我们都会执行以下操作:
<h:inputText id="valMin" tabindex="4" required="false"
maxlength="10" styleClass="valorMinimoMaximo"
value="#{actionGeneralidadeUnidadeCalculo.valorMinimoTransiente}"
size="10" label="ValorMinimo">
</h:inputText>
其中 valorMinimoTransiente
是一个字符串,当它进入操作时,我将其转换为 BigDecimal
。
我这样做是因为我们的十进制数字有逗号,而不是点,当您尝试转换类似的内容时
4,56
对于BigDecimal
,它给出了一个异常。
那么,有没有一种方法无需整个过程即可获取 BigDecimal
值?
顺便说一句,我正在运行 jsf 1.2 和 richfaces 3.3.3。
最佳答案
哎呀!听起来您被迫在旧的应用程序服务器(例如 WebSphere Application Server v6.1)上使用 JSF 1.2。
您可以使用标准 BigDecimalConverter:
<f:converter converterId="javax.faces.BigDecimal" />
但是,您可能会发现该转换器受到限制,具体取决于您的需求。如果是这样,那么最好编写自己的自定义转换器。
因此,请继续阅读如何创建自己的转换器类。
创建转换器类:
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("bigDecimalConverter")
public class BigDecimalConverter implements Converter {
...override and implement the required interface methods here
}
要使用自定义转换器,您可以使用:
<h:inputText value="#{someBean.field}" converter="bigDecimalConverter"/>
或者:
<h:inputText value="#{someBean.field}">
<f:converter converterId="bigDecimalConverter"/>
</h:inputText>
我已经很长时间没有使用 JSF 1.2 了,但是注释可能是 JSF 2.0 约定。如果是这样,您必须以老式方式注册自定义转换器:faces-config.xml:
<converter>
<description>
Some description here (optional)
</description>
<converter-id>bigDecimalConverter</converter-id>
<converter-class>
com.companyname.project.converters.BigDecimalConverter
</converter-class>
</converter>
引用文献:
关于jsf - 在jsf上将输入文本值转换为大十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28607169/