我有一个奇怪的情况,即f:convertNumber在尝试将其存储到无类型地图时不起作用。
我希望将数字存储为Map中的java.lang.Double对象。
这是xhtml摘录:
<h:inputText value="#{tInputBean.generalMap['myNumber']}" id="testNumber">
<f:convertNumber />
</h:inputText>
<p:message for="testNumber" />
这是豆子:
@Named("tInputBean")
@Scope("view")
public class TInputBean {
private Log log = LogFactory.getLog(TInputBean.class);
private Map generalMap = new HashMap();
// .. and the setter getter
....
}
这是我的日志:
DEBUG PhaseTracker - BEFORE PHASE RESTORE_VIEW 1
DEBUG PhaseTracker - AFTER PHASE RESTORE_VIEW 1
DEBUG PhaseTracker - BEFORE PHASE APPLY_REQUEST_VALUES 2
DEBUG DebugUtil - ============== start debugMap parameter map : ==============
DEBUG DebugUtil - testNumber='123123123'
DEBUG DebugUtil - javax.faces.partial.execute='SaveHeader testNumberPanel RPBPanel'
DEBUG DebugUtil - SaveHeader='SaveHeader'
DEBUG DebugUtil - javax.faces.partial.render='messages RPBPanel DebugVersionHeader testNumberPanel'
DEBUG DebugUtil - javax.faces.source='SaveHeader'
DEBUG DebugUtil - j_idt42='j_idt42'
DEBUG DebugUtil - javax.faces.ViewState='7088371747667351331:-3641627426454744246'
DEBUG DebugUtil - transNum='TRX003'
DEBUG DebugUtil - javax.faces.partial.ajax='true'
DEBUG DebugUtil - ============== end debugMap parameter map : ==============
DEBUG PhaseTracker - AFTER PHASE APPLY_REQUEST_VALUES 2
DEBUG PhaseTracker - BEFORE PHASE PROCESS_VALIDATIONS 3
DEBUG PhaseTracker - AFTER PHASE PROCESS_VALIDATIONS 3
DEBUG PhaseTracker - BEFORE PHASE RENDER_RESPONSE 6
DEBUG PhaseTracker - AFTER PHASE RENDER_RESPONSE 6
渲染完成后,<p:message for="testNumber" />
将替换为:{0}: Conversion error occurred.
我注意到的是:
<f:convertNumber>
标记中指定类似java.lang.Double的类型。我应该让自己的转换器工作吗?我已经想象过:
<h:inputText value="#{tInputBean.generalMap['myNumber']}" id="testNumber">
<f:converter type="MyDoubleConverter" />
</h:inputText>
<p:message for="testNumber" />
请就此事分享您的意见。 谢谢!
更新
我已经构建了一个适合我需要的简单转换器,到目前为止它已经正常工作了。以下是代码,请分享您的意见:
@FacesConverter(value="Double")
public class DoubleConverter implements Converter {
private Log log = LogFactory.getLog(DoubleConverter.class);
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
//log.debug("locale : " + context.getViewRoot().getLocale());
DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(context.getViewRoot().getLocale());
//log.debug("VALUE == " + value);
String result = value == null || value.toString().trim().length() == 0 ? null : format.format(value);
//log.debug("RESULT == " + value);
return result;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
//log.debug("locale : " + context.getViewRoot().getLocale());
//log.debug("STRING VALUE == " + value);
DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(context.getViewRoot().getLocale());
try {
Object result = ((value == null) || (value.trim().length() == 0)) ? null : format.parse(value).doubleValue();
//log.debug("RESULT == " + result);
return result;
} catch (Exception e) {
log.error(e,e);
throw new ConverterException(e);
}
}
}
以下是我的jsf如何使用它:
<h:inputText value="#{tInputBean.generalMap['myNumber']}"
id="testNumber" converter="Double"/>
<p:message for="testNumber" />