JSF自定义转换器未调用null值 [英] JSF Custom Converter not called on null value
问题描述
我在输出java.math.BigDecimal时创建了自定义Converter。当BigDecimal为0.00或 null
时,我想输出一个破折号。
I've created custom Converter for when I output java.math.BigDecimal. When the BigDecimal is 0.00 or null
I want output a dash.
这是我的XHTML
<p:dataTable value="#{bean.data}" var="item">
<p:column>
<h:outputText value="#{item.currentValue}">
<f:converter converterId="my.bigDecimalConverter" />
</h:outputText>
</p:column>
</p:dataTable>
我遇到的问题是#{item.currentValue}是 null
转换器中的 getAsString
方法未被调用。
The problem I have is when #{item.currentValue} is null
the getAsString
method in the Converter is not called.
@FacesConverter("my.bigDecimalConverter")
public class BigDecimalConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (context == null || component == null) {
throw new NullPointerException();
}
if (value == null) {
System.out.println("null=");
return "--";
}
System.out.print("Class=" + value.getClass());
if (value instanceof String) {
System.out.println("Str=" + value);
return (String) value;
}
if (value instanceof BigDecimal) {
BigDecimal bd = (BigDecimal)value;
if (bd.equals(new BigDecimal("0.00"))) {
return "--";
} else {
return bd.toPlainString();
}
}
return "";
}
}
我说它没有被叫,因为我没有当BigDecimal为 null
时,错误并且没有 println
语句输出。当BigDecimal不是 null
时,它按预期工作,并且 Class = class java.math.BigDecimal打印出来,当BigDecimal为0.00时我在页面上输出 -
。
I'm saying its not called because I get no errors and no println
statement output when the BigDecimal is null
. When the BigDecimal is not null
it works as expected and "Class=class java.math.BigDecimal" gets printed out and when the BigDecimal is 0.00 I do get --
outputted on the page.
我正在使用JSF 2.1,Mojarra 2.1.27
I'm using JSF 2.1, Mojarra 2.1.27
我还使用以下方法测试我的转换器。
I also using the following to test my converter.
<h:outputText value="#{null}">
<f:converter converterId="my.bigDecimalConverter" />
</h:outputText>
阅读这个问题似乎转换器应该使用 null
价值。
https://stackoverflow.com/a/19093197/50262
Reading over this question it would seem that a converter should work with a null
value.
https://stackoverflow.com/a/19093197/50262
推荐答案
你发布的链接说转换器应该使用null,但是不要说在每种情况下都会调用转换器。
The link you posted says that a converter should work with a null but doen't say a converter will be called in every situation with null values.
具体地说,当在 h里面时,它不会说转换器会被调用h:outputText
,且值为空。
Concretely it don't says a converter will be called when it is inside a h:outputText
and the value is null.
如果你深入了解Mojarra来源,你会看到:
If you dig in the Mojarra sources you'll see:
//Line 355 -- com.sun.faces.renderkit.html_basic.HtmlBasicRenderer
//method getCurrentValue
Object currentObj = getValue(component);
if (currentObj != null) {
currentValue = getFormattedValue(context, component, currentObj);
}
很明显,永远不会转换空值!而且我找不到解决方法。
It's clear that a null value will never be converted! And I couldn't find a workaround.
然后如果你确实需要你的值为null(你可以返回0或者其他)我认为您唯一的机会是制作自定义渲染器。这很简单:
Then if you really need your value to be null (you could return 0 or something) I think your only chance is making a custom renderer. It is very easy:
你编写的渲染器会覆盖重要的方法:
You write a renderer that overrides the method that matters:
package my;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import com.sun.faces.renderkit.html_basic.TextRenderer;
public class HtmlCustomRenderer extends TextRenderer {
@Override
public String getCurrentValue(FacesContext context, UIComponent component) {
if (component instanceof UIInput) {
Object submittedValue = ((UIInput) component).getSubmittedValue();
if (submittedValue != null) {
// value may not be a String...
return submittedValue.toString();
}
}
String currentValue = null;
Object currentObj = getValue(component);
//Remove the 'if' to call getFormattedValue even if null
currentValue = getFormattedValue(context, component, currentObj);
return currentValue;
}
}
然后我们在faces-config.xml中声明渲染器:
And then we declare the renderer in faces-config.xml:
<render-kit>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>javax.faces.Text</renderer-type>
<renderer-class>my.HtmlCustomRenderer</renderer-class>
</renderer>
</render-kit>
现在您的转换器将被调用为空值!
Now your converter will be called with null values!
我希望它会有所帮助!
这篇关于JSF自定义转换器未调用null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!